Bookmark this page

Chapter 2.  Scheduling Future Tasks

Abstract

Goal Schedule tasks to automatically execute in the future.
Objectives
  • Set up a command that runs once at some point in the future.

  • Schedule commands to run on a repeating schedule using a user’s crontab file.

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

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

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

  • Scheduling Recurring User Jobs (and Guided Exercise)

  • Scheduling Recurring System Jobs (and Guided Exercise)

  • Managing Temporary Files (and Guided Exercise)

Lab

Scheduling Future Tasks

Scheduling a Deferred User Job

Objectives

After completing this section, you should be able to set up a command that runs once at some point in the future.

Describing Deferred User Tasks

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 Red Hat Enterprise 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

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

For a complete list of valid time specifications, refer to the timespec definition as listed in the references.

Inspecting and Managing Deferred User 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
128  2Mon Feb  2 05:13:00 2015 3a 4user
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.

1

The unique job number for this job.

2

The execution date and time for the scheduled job.

3

Indicates that the job is scheduled with the default queue a. Different jobs may be scheduled with different queues.

4

The owner of the job (and the user that the job will run as).

Important

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

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

References

at(1) and atd(8) man pages

/usr/share/doc/at/timespec

Revision: rh134-8.2-f0a9756