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
Installation
If at
is not already installed on your system, install the at
package using your operating system package manager (apt
, pacman
, dnf
, etc.). After installed, ensure ‘atd.service’ is started and enabled:
sudo systemctl enable --now atd.service
Usage
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>
Date/time format
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 withAM
orPM
.midnight/noon/teatime
: to run the job at midnight (00:00
), noon (12:00
) or teatime (16:00
).month day
: for example,january 25
. You can also add the year after the day.mmddYYYY
,mm/dd/YYYY
,dd.mm.YYYY
orYYYY-mm-dd
: Some examples are12152022
,03/05/2022
,16.05.2022
and2022-04-23
. Defining the year with two digits is allowed.now + number timeunit
: this format is used in the first example. Allowed time units areminutes
,hours
,days
andweeks
(and their singular form).today/tomorrow
: suffix the time with these units to run the job today or tomorrow at the specified time, for example15:00 tomorrow
.
List jobs
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.
Delete jobs
Run atrm <job number>
to delete a job:
atrm 7
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.allow
and /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.
Latest tutorials and articles:
Featured content: