When Linux administrators need to schedule a task to run automatically, their first instinct is usually to reach for cron. While cron is incredibly powerful, it is specifically designed for recurring tasks (like running a backup script every night at 2 AM). If you only need to run a command or script exactly once at a specific time in the future — perhaps restarting a service during low-traffic hours or executing a database migration while you are asleep — configuring a cron job is unnecessary overhead and requires you to remember to delete the job later.
For one-off scheduled execution, the native Linux at command is the perfect tool. It allows you to queue commands to run once at a designated time, after which the job is automatically deleted from the queue.
Installing the at Command
While at is a standard Unix utility, it is not always installed by default on modern, minimal Linux distributions (particularly cloud server images).
To check if it is installed, simply type at in your terminal and press Enter. If you receive a “command not found” error, you must install it via your package manager.
- On Debian/Ubuntu systems:
sudo apt update && sudo apt install at - On RHEL/CentOS/Fedora systems:
sudo dnf install at
Once installed, you must ensure the atd (at daemon) service is running so it can actually execute your queued jobs:
sudo systemctl enable --now atd
How to Schedule a Command Using at
The syntax for the at command is highly intuitive and accepts a wide variety of human-readable time formats.
Step 1: Define the Execution Time
Begin by invoking the at command followed by the time you want the task to run. For example, to schedule a task for 3:00 PM today, type:
at 3:00 PM
Press Enter. You will notice that your terminal prompt changes to an interactive at> prompt. This indicates you are now inside the at shell, ready to define the commands.
Step 2: Enter the Commands
At the at> prompt, type the command or path to the script you want to execute. For example, to restart the Nginx web server, you would type:
at> sudo systemctl restart nginx
Press Enter. You can queue multiple commands by typing each one on a new line.
Step 3: Save and Exit
When you have finished entering your commands, you must signal to the at daemon that you are done. Do this by pressing Ctrl + D on your keyboard. This sends an EOF (End of File) signal.
The terminal will display a confirmation message indicating the job number and the exact time it will execute:job 1 at Wed Oct 25 15:00:00 2024
Understanding Time Syntax Options
The true power of the at command lies in its flexible time parser. You do not have to use strict 24-hour clocks (though you can). Here are some common ways to format the time argument:
- Specific Time:
at 14:30orat 2:30 PM - Specific Date and Time:
at 9:00 AM July 4orat 09:00 07/04/24 - Relative Time (Minutes/Hours/Days):
at now + 30 minutesorat now + 2 hoursorat now + 1 day - Keywords:
at midnight,at noon, orat teatime(which translates to 4:00 PM).
For example, if you want to run a system update in exactly two hours without calculating the precise time, you simply use:at now + 2 hours
How to Execute an Existing Script
If you have already written a bash script and simply want at to execute it at a later time, you do not need to enter the interactive at> prompt. You can use the -f (file) flag to pass the script directly to the command.
at 10:00 PM -f /path/to/your/script.sh
How to View and Manage Scheduled Jobs
If you schedule a task for the middle of the night, you might want to verify that it is actually in the queue before you log off.
To view all pending jobs in the at queue, use the atq command:
atq
This will output a list of scheduled jobs, showing the job ID number, the scheduled execution time, and the user who created the job.
2 Thu Oct 26 02:00:00 2024 a root
3 Fri Oct 27 15:00:00 2024 a sysadmin
How to Delete a Scheduled Job
If you change your mind or realize you made an error in the command, you can easily cancel a scheduled job using the atrm (at remove) command followed by the Job ID number (which you found using atq).
For example, to cancel Job ID 2, type:
atrm 2
The job is instantly removed from the queue and will not execute.