Scheduling tasks on Linux using the at command

Posted by atyndall on Mon, 22 Nov 2021 08:37:29 +0100

When you want a command or script to run at a specific time, you don't need to hover your finger on the keyboard and wait for the Enter key to be pressed, or sit at your desk at a specific time. Instead, you can set the task with the at command. In this article, we will study how to use at to schedule tasks, how to accurately select when tasks want to run, and how to use at to view scheduled tasks.

at vs cron

For those who use cron to schedule tasks on Linux systems, the at command is similar to cron because you can schedule tasks at a selected time, but cron is used for jobs that run regularly - even once a year. Most cron jobs are run daily, weekly or monthly, but you can control the frequency and time of running.

On the other hand, the at command is used for tasks that run only once. Want to restart the system at midnight? No problem. As long as you have the appropriate permissions, at can do this for you. If you want the system to restart at 2 a.m. every Saturday, use cron instead.

Use at

The at command is easy to use and only needs to remember a few things. A simple example of using at is similar to this:

$ at 5:00PM
at> date >> thisfile
at> <EOT>

When you enter at and the time when the command should run, at will prompt you to run the command at the set time (in this case, the date command). Enter ^ D (Ctrl + d) to complete the request.

If we set the at command before 5 p.m., the date and time will be added to the end of the file named this file at 5 p.m. that day. Otherwise, the command will run at 5 p.m. the next day.

When interacting with the at command, you can enter multiple commands. If you want to run multiple commands at the same time, just enter multiple command lines:

$ at 6:22
warning: commands will be executed using /bin/sh
at> echo first >> thisfile
at> echo second >> thisfile
at> <EOT>

In the above command, we use an ordinary user account and add some simple text to the file in the user's home directory. If you run these commands after 6:22 a.m., they run the next day because 6:22 means 6:22 a.m. If you want to run at 6:22 PM, use 6:22 PM or 18:22 PM. 6: 22 pm this also works.

You can also use at to schedule commands to run on a specified date or time, such as 10:00AM April 15 2021 or noon + 5 days (run at noon within 5 days from today). The following are some examples:

at 6PM tomorrow
at noon April 15 2021
at noon + 5 days
at 9:15 + 1000 days

After specifying the command to run and pressing ^ D, you will notice that the at command assigns a job number to each request, which will be displayed in the job queue of the at command.

$ at noon + 1000 days
warning: commands will be executed using /bin/sh
at> date >> thisfile
at> <EOT>
job 36 at Tue Dec 27 12:00:00 2022        <== job # is 36

Check queue

You can use the atq (at queue) command to view the at job queue:

$ atq
32      Thu Apr  2 03:06:00 2020 a shs
35      Mon Apr  6 12:00:00 2020 a shs
36      Tue Dec 27 12:00:00 2022 a shs
34      Thu Apr  2 18:00:00 2020 a shs

If you need to cancel a job in the queue, use the atrm (at remove) command and job number:

$ atrm 32
$ atq
35      Mon Apr  6 12:00:00 2020 a shs
36      Tue Dec 27 12:00:00 2022 a shs
34      Thu Apr  2 18:00:00 2020 a shs

You can use the at -c command to view the details of the scheduled task. Other details (search path of the activity, etc.) can also be seen, but the last line of the output shows the command scheduled to run.

$ at -c 36 | tail -6
cd /home/shs || {
         echo 'Execution directory inaccessible' >&2
         exit 1
}
date >> thisfile

Note that the command display first tests whether you can enter the user directory through the cd command. If not, the job exits with an error. If possible, run the command specified in at. It treats the command as "go to / home/shs or exit with errors".

Run the job as root

To run the at job as root, simply use sudo with your at command, as follows:

$ sudo at 8PM
[sudo] password for shs:
warning: commands will be executed using /bin/sh
at> reboot now
at> <EOT>
job 37 at Wed Apr  1 16:00:00 2020

Note that root's tasks are displayed in the queue with root as the performer.

35      Mon Apr  6 12:00:00 2020 a shs
36      Tue Dec 27 12:00:00 2022 a shs
37      Wed Apr  1 20:00:00 2020 a root         <==

Run script

You can also use the at command to run the script. Here is an example:

$ at 4:30PM
warning: commands will be executed using /bin/sh
at> bin/tryme
at> <EOT>

The at command is prohibited

/The / etc.deny file provides a way to prevent users from using the at command. By default, it may contain a list of accounts that are not allowed, such as ftp and nobody. You can use the / etc/at.allow file to do the opposite, but usually only the at.deny file is configured.