- 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
IntroductionDuring 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
atprogram 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,
atgives 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).
Installing atIf not installed by default,
atshould be available in almost all distributions' repositories.
To install it on Fedora, just run:
# dnf install atOn RHEL or CentOS yum is still the default package manager:
# yum install atOn Debian or Ubuntu:
# apt-get install atOn Archlinux:
# pacman -S at
Starting the daemonOnce the program it's installed, we must start the
atddaemon 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 promptWith 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 minuteTo run the same command at 4pm, three days from now, instead, we would run:
$ at 4pm + 3 daysOnce the above line is executed, the
atprompt 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>To exit the
job 4 at Tue Dec 19 11:29:00 2017
atprompt we should press the
CTRL+dkey 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
atworks. 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 scriptInstead of specifying the command to be executed, interactively, from the prompt, we can instruct
atto execute an existing script or program simply by passing it as an argument to the
-fflag 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 jobsTo queue, examine or delete jobs scheduled with at, we can either use dedicated commands like
atwith 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 egdocThe 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
atwith 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
ConclusionsAlthough simpler than
atprogram 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.