Run a command at a later time with 'at'
When you need to execute a script in a future date, you can use the simple ‘at’ command for that.
Table of Contents
at is not already installed on your system, install the
at package using your operating system package manager (
dnf, etc.). After installed, ensure ‘atd.service’ is started and enabled:
sudo systemctl enable --now atd.service
This is a simple example of how to use
at to run a script (
run.sh) at the next minute:
at -f run.sh now + 1 minute
-f: reads the job from file instead of standard input.
$ at -f run.sh now + 1 minute warning: commands will be executed using /bin/sh job 3 at Fri Oct 14 13:46:00 2022
As you can see,
at syntax is:
at [options] <timespec>
There are several ways to define a datetime:
HH:MM: define the time of the day. If the time has passed, the next day is assumed.
AM/PM: you can suffix a time with
midnight/noon/teatime: to run the job at midnight (
00:00), noon (
12:00) or teatime (
month day: for example,
january 25. You can also add the year after the day.
YYYY-mm-dd: Some examples are
2022-04-23. Defining the year with two digits is allowed.
now + number timeunit: this format is used in the first example. Allowed time units are
weeks(and their singular form).
today/tomorrow: suffix the time with these units to run the job today or tomorrow at the specified time, for example
atq lists user’s pending jobs. If executed as superuser, lists everybody’s jobs.
$ atq 7 Sat Oct 15 12:00:00 2022 a ricardo 10 Sat Oct 15 15:00:00 2022 a ricardo 5 Sun Dec 25 16:31:00 2022 a ricardo
- First number is the job number, you will need it to delete the job.
atrm <job number> to delete a job:
Allow or deny access to users
You can define which users can create jobs with
at. If the
/etc/at.allow exists, only usernames mentioned there are allowed. If the file does not exists,
/etc/at.deny is checked, and every username not mentioned there is allowed. The content of
/etc/at.deny is the same, one line per username allowed or denied. Only the root user (or any user with superuser privileges) can read and write these files.
$ sudo head /etc/at.deny alias backup bin daemon [output truncated]
If you have any suggestion, feel free to contact me via social media or email.