Americas

  • United States
sandra_henrystocker
Unix Dweeb

Using the at command to schedule tasks on Linux

How-To
Mar 15, 20233 mins
Linux

Time displayed on Amazon Echo Dot with Clock (5th Gen)

To schedule a command or script to run at some particular time, the at command is perfect and provides many options for specifying the time you want it to run. It will set the task up to be run whenever you specify, and you can view the scheduled tasks or even change your mind and cancel one of them as you see fit.

The at command differs from cron in that it sets up a command or script to run only once, while cron allows you to set up commands or scripts to be run on a specified schedule – whether every day, once a week, a couple times a month or even just once a year.

at command syntax

Using the at command is relatively easy, though it has a lot of options, particularly on how you specify the time a task should be run. If you specify a time like shown below, the task will be set up to be run the next time you reach 15:27 (3:27 PM), whether that’s today or tomorrow.

$ at 15:27
warning: commands will be executed using /bin/sh
at Wed Mar 15 15:27:00 2023
at> echo "It works!" > /tmp/test
at> 
job 5 at Wed Mar 15 15:27:00 2023

End the at command by pressing ^D.

In the next example, the command is set to run the following date at the time specified.

$ at 12:34 tomorrow
warning: commands will be executed using /bin/sh
at Wed Mar 15 12:34:00 2023
at> echo 1234 >> /tmp/wow
at> 
job 6 at Wed Mar 15 12:34:00 2023

You can specify the time of day in either of two formats. If you ask to have a command run at 11:11, it will be run at 11:11 AM. To specify it to run at 11:11 PM, use 23:11 or 11:11pm as the time.

Dates can be also specified in any of these formats:

MMDD[CC]YY
MM/DD/[CC]YY
DD.MM.[CC]YY
[CC]YY-MM-DD

You can also specify a task on some particular date by specifying a weekday (e.g., Sunday).

$ at noon Wednesday
warning: commands will be executed using /bin/sh
at Wed Mar 15 12:00:00 2023
at> date >> /tmp/test
at> 
job 7 at Wed Mar 15 12:00:00 2023

Listing at jobs

To list the tasks scheduled with the at command, use the atq (at queue) command.

$ atq
5      Wed Mar 15 15:27:00 2023 a shs
6      Wed Mar 15 12:34:00 2023 a shs
7      Wed Mar 15 12:00:00 2023 a shs

The command shows the jobs waiting to be run, but not the tasks they will be running. The details only include the job numbers, the times they will run and the user who added them. Only root is able to view all of the scheduled tasks. Other users will see only their own.

Deleting at jobs

You can cancel a scheduled job with the atrm command.

$ atrm 5
$ atq
6      Wed Mar 15 12:34:00 2023 a shs
7      Wed Mar 15 12:00:00 2023 a shs

The command below provides a list of commands associated with the at command.

$ sudo dnf repoquery -l at | grep bin
[sudo] password for shs:
Last metadata expiration check: 1:16:38 ago on Fri 10 Mar 2023 01:40:19 PM EST.
/usr/bin/at
/usr/bin/atq
/usr/bin/atrm
/usr/bin/batch
/usr/sbin/atd
/usr/sbin/atrun
/usr/bin/a

Wrap-up

Regularly scheduled tasks should be set up using the cron command, but to run some particular task at some particular time (no repeats), the at command is flexible and easy..

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author