Abstract
| Goal | Schedule tasks to execute at a specific time and date. |
| Objectives |
|
| Sections |
|
Sometimes you might need to run one or more commands at a specific future time. An example is a user who schedules a long-running maintenance task to occur in the middle of the night. Another example is a system administrator who is working on a firewall configuration and queues a safety job to reset the firewall settings to a former working state in ten minutes' time. The system administrator then deactivates the job before it runs, unless the new firewall configuration worked.
These scheduled commands are called tasks or jobs, and the deferred term indicates that these tasks run in the future.
One available solution for Red Hat Enterprise Linux users to schedule deferred tasks is the at command, which is installed and enabled by default.
The at package provides the atd system daemon and the at and atq commands to interact with the daemon.
Any user can queue jobs for the atd daemon by using the at command.
The atd daemon provides 26 queues, identified from a to z, where jobs in alphabetically later queues get lower system priority (with higher nice values, as discussed in a later chapter).
Use the at command to start entering a new job to schedule.
The TIMESPECat command then reads from STDIN (your keyboard) to obtain the commands to run.
When manually entering commands, complete the input by pressing Ctrl+D on an empty line.
You can use input redirection from a script file for entering more complex commands.
For example, use the at now + command to schedule the commands in 5min < myscriptmyscript to start in 5 minutes, without needing to type the commands manually in a terminal window.
The at command TIMESPEC argument accepts natural time specifications to describe when a job should run.
For example, specify a time as 02:00pm, 15:59, midnight, or even teatime, followed by an optional date or number of days in the future.
The TIMESPEC argument expects time and date specifications in that order.
If you provide the date and not the time, then the time defaults to the current time.
If you provide the time and not the date, then the date is considered to be matched, and the jobs run when the time next matches.
The following example shows a job schedule without providing the date.
The at command schedules the job for today or tomorrow depending whether the time has passed.
[user@host ~]$dateWed May 18 21:01:18 CDT 2022 [user@host ~]$at 21:03 < myscriptjob 3 at Wed May 18 21:03:00 2022 [user@host ~]$at 21:00 < myscriptjob 4 at Thu May 19 21:00:00 2022
The man pages for the at command and other documentation sources use lowercase to write the natural time specifications.
You can use lowercase, sentence case, or uppercase.
Here are examples of time specifications that you can use:
now +5min
teatime tomorrow (teatime is 16:00)
noon +4 days
5pm august 3 2021
For other valid time specifications, refer to the local timespec document listed in the references.
For an overview of the pending jobs for the current user, use the atq or the at -l command.
[user@host ~]$ atq
28 Mon May 16 05:13:00 2022 a user
29 Tue May 17 16:00:00 2022 h user
30 Wed May 18 12:00:00 2022 a userIn the preceding output, every line represents a different scheduled future job. The following description applies to the first line of the output:
28
is the unique job number.
Mon May 16 05:13:00 2022
is the execution date and time for the scheduled job.
a
indicates that the job is scheduled with the default queue a.
user
is the owner of the job (and the user that the job runs as).
Unprivileged users can view and manage only their own jobs.
The root user can view and manage all jobs.
Use the at -c command to inspect the commands that run when the JOBNUMBERatd daemon executes a job.
This command shows the job's environment, which is set from the user's environment when they created the job, and the command syntax to run.