Learning how schedule and manage tasks using the at program
- Root permissions to start the atd daemon
- Having the at program installed
- # – requires given linux commands to be executed with root privileges either
directly as a root user or by use of
- $ – requires given linux commands to be executed as a regular non-privileged user
During the administration of a system, being able to schedule a task for a later execution it’s one crucial ability: to perform a backup of a database for example, or perhaps to run a maintenance script. Less known than
at program let us do this in a pretty easy way: in this tutorial we will learn how to use it and how it is different from the programs mentioned above.
What is at?
Unlike cron, which let us run a task on a regular basis,
at gives us the ability to execute a command or a script at a specified date and hour, or after a given interval of time. Minutes, hours, days or weeks can be used as units. It’s even possible to use certain “keywords” as
teatime (which corresponds to 4pm).
If not installed by default,
at should be available in almost all distributions’ repositories.
To install it on Fedora, just run:
# dnf install at
On RHEL or CentOS yum is still the default package manager:
# yum install at
On Debian or Ubuntu:
# apt-get install at
# pacman -S at
Starting the daemon
Once the program it’s installed, we must start the
atd daemon and eventually enable it if we want it to be launched automatically at boot. I will here assume the use of systemd as the init system. The command must be executed with root privileges:
# systemctl enable --now atd.service
Scheduling a job from the at prompt
With everything in place, we can now use
at. Let’s suppose we want to run a command 1 minute from now. The correct syntax would be:
$ at now + 1 minute
To run the same command at 4pm, three days from now, instead, we would run:
$ at 4pm + 3 days
Once the above line is executed, the
at prompt will appear, waiting for us to enter the command to be executed after the specified time interval:
$ at now + 1 minutes at> echo "Hello world" > test.txt at> job 4 at Tue Dec 19 11:29:00 2017
To exit the
at prompt we should press the
CTRL+d key combination. At this point we will presented with a summary of the scheduled task, which will show us the
job id (4 in this case) and the date at which it will be executed.
Just as an example, we entered a trivial command to show how
at works. A minute from now, the “Hello world” string will be written to the file test.txt, which will be automatically created if doesn’t already exist.
Schedule the execution of a script
Instead of specifying the command to be executed, interactively, from the prompt, we can instruct
at to execute an existing script or program simply by passing it as an argument to the
-f flag or, alternatively, by using the
< redirection operator. Therefore, assuming we want to run a script which is present in our current working directory, we would run:
# Using the dedicated -f flag $ at now + 1 minute -f script.sh
# Using the < redirection operator $ at now + 1 minute < script.sh
Manage scheduled jobs
To queue, examine or delete jobs scheduled with at, we can either use dedicated commands like
atq or run
at with specific flags, the latter being just aliases for the former. For example, say we want to obtain a list of all pending jobs scheduled with at by our user:
$ atq 4 Tue Dec 19 11:29:00 2017 a egdoc
The above command, if launched as root, will display the task scheduled by all users in the system.
To delete a queued job, we could use
atrm or run
at with the equivalent flags:
-d. The job to be deleted must be referenced by its number. In the case above, we would therefore run:
$ atrm 4
Although simpler than
at program can be very useful in certain situations: to run a program with a specific delay or when you know exactly the time in which the task must be executed. Reference the manual for further information, and add this little tool to your toolbox, it will surely come in handy.