Bookmark this page

Chapter 2.  Schedule Future Tasks

Abstract

Goal

Schedule tasks to execute at a specific time and date.

Objectives
  • Set up a command to run once at a future time.

  • Schedule commands to run on a repeating schedule with a user's crontab file.

  • Schedule commands to run on a repeating schedule with the system crontab file and directories.

  • Enable and disable systemd timers, and configure a timer that manages temporary files.

Sections
  • Schedule a Deferred User Job (and Guided Exercise)

  • Schedule Recurring User Jobs (and Guided Exercise)

  • Schedule Recurring System Jobs (and Guided Exercise)

  • Manage Temporary Files (and Guided Exercise)

  • Schedule Future Tasks (Quiz)

Schedule a Deferred User Job

Objectives

  • Set up a command to run once at a future time.

Describe Deferred User Tasks

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).

Schedule Deferred User Tasks

Use the at TIMESPEC command to start entering a new job to schedule. The at 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 +5min < myscript command to schedule the commands in myscript 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 ~]$ date
Wed May 18 21:01:18 CDT 2022
[user@host ~]$ at 21:03 < myscript
job 3 at Wed May 18 21:03:00 2022
[user@host ~]$ at 21:00 < myscript
job 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.

Inspect and Manage Deferred User Jobs

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 user

In 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).

Important

Unprivileged users can view and manage only their own jobs. The root user can view and manage all jobs.

Use the at -c JOBNUMBER command to inspect the commands that run when the atd 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.

Remove Jobs from Schedule

The atrm JOBNUMBER command removes a scheduled job. Remove the scheduled job when you no longer need it, for example, when a remote firewall configuration succeeded, and you do not need to reset it.

References

at(1) and atd(8) man pages

/usr/share/doc/at/timespec

Revision: rh134-9.3-5fd2368