Skip to content

at Command: Schedule Commands for Later Execution

Summary

The at command allows you to schedule commands or scripts to be executed at a specific time in the future. It's useful for automating tasks, running batch jobs during off-peak hours, or simply delaying the execution of a command.

Introduction

The at command is a powerful tool for scheduling tasks in Linux. Unlike cron, which schedules recurring tasks, at is designed for one-time executions. It reads commands either from standard input or from a specified file and queues them for execution at the designated time. The at daemon (atd) is responsible for monitoring the queue and executing the commands at their scheduled times.

Use case and Examples

Schedule a command to run in 10 minutes

at now + 10 minutes
Press Enter. You will be prompted to enter the command(s) you want to run. For example:
echo "Hello from the future!" > future.txt
<EOT>
Press Ctrl+D to signal the end of the input. This schedules the command echo "Hello from the future!" > future.txt to be executed 10 minutes from the current time. The output will be redirected to a file called future.txt.

Schedule a command to run at 3 PM tomorrow

at 3pm tomorrow
Press Enter, then enter your command(s) and press Ctrl+D:
/path/to/my/script.sh
<EOT>
This will execute the my/script.sh script at 3 PM on the next day.

Schedule a command to run on a specific date

at 2024-03-15 10:00
Press Enter, then enter your command(s) and press Ctrl+D:
rm -rf /tmp/temp_files
<EOT>
This will remove the /tmp/temp_files directory (and its contents) on March 15, 2024, at 10:00 AM. Be very careful with the rm -rf command!

Read commands from a file

at -f my_commands.txt now + 2 hours
This schedules the commands contained in the my_commands.txt file to be executed two hours from the current time. The my_commands.txt file should contain one or more commands, each on a separate line.

List pending at jobs

atq
This command displays a list of all the jobs currently scheduled by the at command, along with their job IDs and scheduled execution times.

Remove a scheduled at job

atrm <job_id>
Replace <job_id> with the actual job ID you want to remove. You can get the job ID from the output of atq. For example: atrm 3 would remove job ID 3.

Commonly used flags

Flag Description Example
-f Specifies a file from which to read commands. at -f my_script.sh now + 1 hour
-m Sends an email to the user when the command has completed. at -m 5pm tomorrow (followed by commands and Ctrl+D)
-t Specifies the time using the [[CC]YY]MMDDhhmm[.ss] format. at -t 202403011200.00 (for March 1, 2024 at noon)
-l Lists pending jobs (same as atq). at -l
-d Deletes a pending job (same as atrm). at -d 5 (deletes job ID 5)
-v Show the time the job will be executed. at -v now + 2 days


Share on Share on

Comments