Schedule tasks in Linux with the "at" command
At, atq and atrm Commands
The "at" command allows jobs to be executed in the future at a predefined date or time. Generally the "at" command is used to schedule your job/script, "atq" is used to show jobs waiting to run and "atrm" is used to remove jobs from the queue. "at" is used normally when you need to run a job only once at a certain time/date.
at command examples
In the following example, I have created a simple script called "at.sh" that will create a file called "hello.txt" in the directory "/tmp". The script will then run the "date" command and append its output into the file.
Content of the script
#!/bin/bash echo "hello" > /tmp/hello.txt date >> /tmp/hello.txt
Before we can run the script, we must set the "execute" permissions by issuing the "chmod +x at.sh" command.
# chmod +x at.sh root@mint01a:/tmp# ls -l at.sh -rwxr-xr-x 1 root root 65 Jun 1 20:46 at.sh
Next we need to schedule the job to run at some time in the future. In this example, I am going to schedule the job to run at "20:59".
# echo "sh at.sh" | at 20:59 warning: commands will be executed using /bin/sh job 9 at Tue Jun 1 20:59:00 2021
You can use the "atq" command to display scheduled jobs.
# atq 9 Tue Jun 1 20:59:00 2021 a root
From the above we can see that our job is waiting to run at the specified time of "20:59". Job number "9" has been assigned to this job. All job numbers are in the very first column.
At approximately "20:59" our job will have run and we can see the output that has been created below:
# pwd /tmp # ls -l hello.txt -rw-r--r-- 1 root root 35 Jun 1 20:59 hello.txt # cat hello.txt hello Tue 1 Jun 20:59:00 BST 2021
If you need to cancel a job that has been scheduled to run, you can use the "atrm" command. In the example below, we have three jobs scheduled. If we wanted to cancel one of those jobs, we would issue the "atrm" command followed by the job number.
# atq 10 Wed Jun 2 20:05:00 2021 a root 12 Wed Jun 2 20:15:00 2021 a root 11 Wed Jun 2 20:10:00 2021 a root # atrm 12 root@mint01a:/tmp# atq 10 Wed Jun 2 20:05:00 2021 a root 11 Wed Jun 2 20:10:00 2021 a root
In the above output, you can see that job "12" was cancelled. Now when we issue the "atq" command, we can see the job has now been removed.
If you wanted to remove multiple jobs, you can issue the "atrm" command followed by the job numbers you want to cancel.
# atq 10 Wed Jun 2 20:05:00 2021 a root 11 Wed Jun 2 20:10:00 2021 a root # atrm 10 11 # atq
at time options
The "at" command accepts times in the form of HH:MM to run a job at a specific time of day.
(If that time is already past, the next day is assumed.)
You may also specify midnight, noon, or teatime (4pm) and you can have a time-of-day suffixed with AM or PM for running in the morning or the evening.
You can also say what day the job will be run, by passing a date in the form month-name day with an optional year. You can also specify the date in the following formats
You can also give times like now + count time-units, where the time-units can be minutes, hours, days, or weeks. You can tell "at" to run the job today by suffixing the time with today and to run the job tomorrow by suffixing the time with tomorrow.
The "root" user is able to run the "at" command, however, normal users will need to have the necessary privileges granted. The location of these privileges can be found in "/etc/at.allow" and "/etc/at.deny" files.
The files "/etc/at.allow" and "/etc/at.deny" determine which users can submit commands for later execution.
The format of these files is a list of user names, one per line. White space is not permitted.
If the file "/etc/at.allow" exists,then only user names mentioned in it are allowed to use "at".
If the file "/etc/at.allow" does not exist, then the file "/etc/at.deny" is checked, every username not mentioned in it is then allowed to use at.
If the file "/etc/at.deny" is empty, then this means that every user may use the "at" command.
If neither of the files exist, then only "root" is allowed to use "at".