How to schedule tasks using at command on Linux

Requirements

Root permissions to start the atd daemon

Having the at program installed

Difficulty

Conventions

# - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command

- requires given linux commands to be executed with root privileges either directly as a root user or by use of command $ - requires given linux commands to be executed as a regular non-privileged user

Introduction

cron

anacron

at

What is at?

at

midnight

teatime

SUBSCRIBE TO NEWSLETTER

Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.

Installing at

at

# dnf install at

# yum install at

# apt-get install at

# pacman -S at

Starting the daemon

atd

# systemctl enable --now atd.service

Scheduling a job from the at prompt

at

$ at now + 1 minute

$ at 4pm + 3 days

at

$ at now + 1 minutes at> echo "Hello world" > test.txt at> job 4 at Tue Dec 19 11:29:00 2017

at

CTRL+d

job id

at

Schedule the execution of a script

at

-f

<

# Using the dedicated -f flag $ at now + 1 minute -f script.sh

Manage scheduled jobs

atrm

atq

at

$ atq 4 Tue Dec 19 11:29:00 2017 a egdoc

atrm

at

-r

-d

$ atrm 4

Conclusions

cron

anacron

at

Learning how schedule and manage tasks using the at programEASYDuring 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 thanor, theprogram 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.Unlike cron, which let us run a task on a regular basis,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" asor(which corresponds to 4pm).If not installed by default,should be available in almost all distributions' repositories.To install it on Fedora, just run:On RHEL or CentOS yum is still the default package manager:On Debian or Ubuntu:On Archlinux:Once the program it's installed, we must start thedaemon 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:With everything in place, we can now use. Let's suppose we want to run a command 1 minute from now. The correct syntax would be:To run the same command at 4pm, three days from now, instead, we would run:Once the above line is executed, theprompt will appear, waiting for us to enter the command to be executed after the specified time interval:To exit theprompt we should press thekey combination. At this point we will presented with a summary of the scheduled task, which will show us the(4 in this case) and the date at which it will be executed.Just as an example, we entered a trivial command to show howworks. 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.Instead of specifying the command to be executed, interactively, from the prompt, we can instructto execute an existing script or program simply by passing it as an argument to theflag or, alternatively, by using theredirection operator. Therefore, assuming we want to run a script which is present in our current working directory, we would run:# Using the < redirection operator $ at now + 1 minute < script.shTo queue, examine or delete jobs scheduled with at, we can either use dedicated commands likeandor runwith 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: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 useor runwith the equivalent flags:or. The job to be deleted must be referenced by its number. In the case above, we would therefore run:Although simpler thanor, theprogram 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.