How to Schedule Jobs to run using "at" Command in Linux
Why we need at instead of cron to schedule jobs
Sometimes you might need to run a command, or set of commands, at a set point in the future. Examples include people who want to schedule an email to their boss, or a system administrator working on a firewall configuration who puts a “safety” job in place to reset the firewall settings in ten minutes’ time, unless they deactivate the job beforehand.
These scheduled commands are often called tasks or jobs, and the term deferred indicates that these tasks or jobs are going to run in the future. One of the solutions available to Linux users for scheduling deferred tasks is at. The at package provides the (atd) system daemon along with a set of command-line tools to interact with the daemon (at, atq, and more). In a default Red Hat Enterprise Linux installation, the atd daemon is installed and enabled automatically.
Users (including root) can queue up jobs for the atd daemon using the at command. The atd daemon provides 26 queues, a to z, with jobs in alphabetically later queues getting lower system priority (higher nice values, discussed in a later chapter).
Scheduling Deferred User Tasks with at
Use the at TIMESPEC command to schedule a new job. The at command then reads the commands to execute from the stdin channel. While manually entering commands, you can finish your input by pressing Ctrl+D. For more complex commands that are prone to typographical errors, it is often easier to use input redirection from a script file, for example, at now +5min < myscript, rather than typing all the commands manually in a terminal window.
The TIMESPEC argument with the at command accepts many powerful combinations, allowing users to describe exactly when a job should run. Typically, they start with a time, for example, 02:00pm, 15:59, or even teatime, followed by an optional date or number of days in the future. The following lists some examples of combinations that can be used.
- now +5min
- teatime tomorrow (teatime is 16:00)
- noon +4 days
- 5pm august 3 2021
Inspecting and Managing at jobs
To get an overview of the pending jobs for the current user, use the command atq or the at -l commands.
[user@host ~]$ atq 28 Mon Feb 2 05:13:00 2015 a user 29 Mon Feb 3 16:00:00 2014 h user 27 Tue Feb 4 12:00:00 2014 a user
In the preceding output, every line represents a different job scheduled to run in the future.
- 28 - The unique job number for this job.
- Mon Feb 2 05:13:00 - The execution date and time for the scheduled job.
- a - Indicates that the job is scheduled with the default queue a. Different jobs may be scheduled with different queues.
- user - The owner of the job (and the user that the job will run as).
To inspect the actual commands that will run when a job is executed, use the at -c JOBNUMBER command. This command shows the environment for the job being set up to reflect the environment of the user who created the job at the time it was created, followed by the actual commands to be run
Removing at Jobs
The atrm JOBNUMBER command removes a scheduled job. Remove the scheduled job when it is no longer needed, for example, when a remote firewall configuration succeeded, and does not need to be reset.