How to Use ‘at’ Command to Schedule a Task on Given or Later Time in Linux
As an alternative to cron job scheduler, the at command allows you to schedule a command to run once at a given time without editing a configuration file.
The only requirement consists of installing this utility and starting and enabling its execution:
# yum install at [on CentOS based systems] $ sudo apt-get install at [on Debian and derivatives]
Next, start and enable the at service at the boot time.
--------- On SystemD --------- # systemctl start atd # systemctl enable atd --------- On SysVinit --------- # service atd start # chkconfig --level 35 atd on
Once atd is running, you can schedule any command or task as follows. We want to send 4 ping probes to www.google.com when the next minute starts (i.e. if it’s 22:20:13, the command will be executed at 22:21:00) and report the result through an email ( -m , requires Postfix or equivalent) to the user invoking the command:
# echo "ping -c 4 www.google.com" | at -m now + 1 minute
If you choose to not use the -m option, the command will be executed but nothing will be printed to standard output. You can, however, choose to redirect the output to a file instead.
In addition, please note that at not only allows the following fixed times: now, noon (12:00), and midnight (00:00), but also custom 2-digit (representing hours) and 4-digit times (hours and minutes).
To run updatedb at 11 pm today (or tomorrow if the current date is greater than 11 pm), do:
To shutdown the system at 23:55 today (same criteria as in the previous example applies):
# echo "shutdown -h now" | at -m 23:55
You can also delay the execution by minutes, hours, days, weeks, months, or years using the + sign and the desired time specification as in the first example.
Time specifications are subject to the POSIX standard.
Summary
As a rule of thumb, use at instead of cron job scheduler whenever you want to run a command or execute a given task at a well-defined time only once. For other scenarios, use cron.
Next, we shall cover how to encrypt tar archive files using openssl, till then stay connected to Tecmint.
Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.
Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.
Related Posts
5 thoughts on “How to Use ‘at’ Command to Schedule a Task on Given or Later Time in Linux”
Good article, Gabriel! Just my 5 cents to it. “at” can be used to start something on background, like nohup staff, but more short:
$ echo “rsync remote_resource local_resource” | at now
or
$ at -f /path/to/my/script -m now
just notice “-f script” option – it’s very convenient to run more complex staff “at” can be used instead of cron to run something repeatedly after the first execution ends. This can eliminate cronjobs, scheduled to run each minute with various locks to avoid to run several commands simultaneously. One can add at the end of its script:
—— bash code snippet —–
# script payload here
this_script_name=$(basename $0)
this_script_path=$(readlink –canonicalize $(dirname $0))
at -f $this_script_path/$this_script_name now
—— bash code snippet —–
this will start the script again when it’s finished. Another interesting application that’s unable to implement with cron is periodically execution with random intervals. Mostly the same as abouve, but just add random delay:
—— bash code snippet —–
# script payload here
this_script_name=$(basename $0)
this_script_path=$(readlink –canonicalize $(dirname $0))
MIN_DELAY=15
RANDOM_BIAS=20
delay=$(( ($RANDOM % $RANDOM_BIAS) + $MIN_DELAY ))
at -f $this_script_path/$this_script_name now + $delay min
—— bash code snippet —– That’s all 🙂 Reply
How to run a script at a certain time on Linux? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
I have a text file containing a specific date and time. I want to be able to run a script at the time specified in that file. How would you achieve that? Create another script that runs in background (sort of a deamon) and checks every second if the current time is matching the time in the file? Is there another way? The machine is a linux server , Debian wheezy. Thanks in advance
Why is this topic closed?? It’s a very reasonable question. Google search lead you to this as #1 And the answers are pretty nerdisch. So someone who can answer this is clear English will be blocked
@Richard I suspect the reason StackOverflow is closing so many questions like this is to encourage users to put questions that are off-topic here on the appropriate StackExchange site. There are quite a few new SE sites that previously didn’t exist. For example, there are quite a few very useful Vim questions on SO that have been closed as off topic, and now that there’s a Vi/Vim SE, those questions would be on topic there. Just musing. However, this still does seem like a programming question
Agreed. Very useful topic. Anyone knows an alternative for ‘at’ ? I want to use it on a RHEL server and I don’t want to create a cron job. Just for future reference, cause I need it now, so I guess I will just use the good old ‘sleep’ ‘At’ is convenient because you don’t have to do the calculation yourself (which has led to miscalculations in the past.)
4 Answers 4
This code line executes «ls -l» at a specific time. This is an example of executing something (a command in my example) at a specific time. «at» is the command you were really looking for. You can read the specifications here:
You need the atd daemon running to use at . On Manjaro OpenRC, you can just install at-openrc and add the daemon atd service with: sudo rc-update add atd and start with sudo rc-service atd start . Usually the at package had already included a systemd (the default init/service system on various distro linux including Ubuntu) service which it can be started with sudo systemctl start atd and enable autostart on init with sudo systemctl enable atd .
BTW, my bash documentation describes a at -c usage. What’s it for, if the way to execute a command is to pipe it into std in?
To me this was not helpful. At just claims that the respective command will be executed — but it actually never does so, not matter what command I use (e.g. the invocation of another program), it just claims to do it, but nothing ever happens. Also following Manoel’s advice (which actually sounds so elementary that it belongs into the solution itself) did not help. My ubuntu (21.04) claims that «at-openrc» doesn’t exist and thus can’t be installed. Can’t this response be improved to get an actual running/working example?