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).

For example,

To run updatedb at 11 pm today (or tomorrow if the current date is greater than 11 pm), do:

# echo "updatedb" | at -m 23

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.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Gabriel Cánepa

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.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

5 Responses

  1. Santosh says:

    How to add date along with time.

  2. Jalal Hajigholamali says:

    Hi,
    Very useful article

    remove extra ‘d ‘ from atdd

    # chkconfig –level 35 atdd on

  3. Marcelo says:

    Very useful, thank you!

  4. Sergey Podushkin says:

    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 :)

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.