Skip to main content

Using at for single-use cron jobs in Linux

Scheduling a single job is easy with the at command and can be done without going cron crazy. Find out how.

Image by Gerd Altmann from Pixabay

Cron is one of the most recognizable UNIXisms of the computer world. Even IT people who don't administer Linux servers have heard of that mysterious beast, the "cron job." And Linux admins know that cron jobs are endlessly useful. In fact, it's arguable that the cron system was an early progenitor of the automation craze. If there was something you knew a human could forget to do, cron was the answer.

However, where cron excels is repetition, and sometimes you don't need a job to run every hour or every day or every week. You just need a job to be executed on schedule, and setting an alarm to remind yourself just doesn't seem like the UNIX way. What you need is the at command, a small utility you can use to create a queue of jobs scheduled to run at a specific time in the future.


Before using the at command, you must have a shell script you want to launch at some time. As usual, your script should have a "magic cookie" or "shebang" line at the top of the file to set which shell to use for its execution.
For this example, create a simple script that creates a file in /tmp:


DATE=`date --utc +%s`
echo "hello world $DATE" | tee /tmp/at.log
exit 0

Make the script executable and then give it a test run:

$ chmod +x
$ ./

View the contents of the /tmp/at.log file the script created:

$ cat /tmp/at.log

hello world 1588482185

Scheduling a job

You can schedule a job using a timestamp or with natural language. If you use natural language or simple time formats, then no option is required before specifying the time.
For instance, to schedule to run immediately, you can just use the keyword now:

$ at now -f

warning: commands will be executed using /bin/sh
job 1 at Mon Feb 24 01:23:00 2020
$ cat /tmp/at.log

hello world 1588482620

You can set an offset from now using minutes, hours, days, weeks, or years.

$ at now + 1 day -f

warning: commands will be executed using /bin/sh
job 2 at Tue Feb 25 01:27:00 2020

The years keyword is undocumented, and seconds is not supported. You can specify common times, too, such as midnight, noon, and teatime (that's 16:00).

Simple times also work:

$ at 21:12 -f

warning: commands will be executed using /bin/sh
job 3 at Mon Feb 24 21:12:00 2020

To schedule a calendar date at a specific time, you must use the -t option and any POSIX-compliant time format. For example, using the YYYYMMDDHHMM format:

$ at -t 202003141509 -f

warning: commands will be executed using /bin/sh
job 4 at Sat Mar 14 15:09:00 2020

[ Looking for more advanced system automation? Get started with The Automated Enterprise, a free book from Red Hat. ]


You can send commands to at through a UNIX pipe:

$ echo "hello world" > /tmp/at.log | at now

warning: commands will be executed using /bin/sh
job 5 at Mon Feb 24 01:28:00 2020

$ cat /tmp/at.log

hello world

Batch jobs

The batch command (or at -b) executes a command as early as system resources allow. If your system load is high, you can use batch to enqueue your job to be run when there are CPU cycles to spare.

$ echo "Cycles to spare" > /tmp/at.log | batch

warning: commands will be executed using /bin/sh
job 5 at Mon Feb 24 01:31:00 2020

Viewing your queue

The atq command displays your at queue. This gives you the job ID, the time each job is scheduled to run, the queue each job is grouped into (a for the at queue or b for the batch queue), and the username of the queue owner. The queue owner is usually yourself, unless you're running atq as root, in which case you see all user's at queues.

$ atq 

2   Tue Feb 25 01:27:00 2020  a  seth
3   Mon Feb 24 21:12:00 2020  a  seth
4   Sat Mar 14 15:09:00 2020  a  seth
5   Mon Feb 24 01:31:00 2020  b  seth

You can create and name your own queues using any single character c-z or A-Z. Both a and b are reserved designations for at and batch, and any queue with an uppercase letter is treated as a batch job. Queues with names that alphabetically follow a and b are run with increasing niceness.

Previewing your job

When you send a command or script to at, your current working directory, environment (excluding BASH_VERSINFO, DISPLAY, EUID, GROUPS, SHELLOPTS, TERM, UID, and _), and the umask are retained. If your command expects specific environment settings, then you should set those while passing the command to at or override them in your script.

To see how your command is going to run, use the -c option along with the job number:

$ at -c 4

# atrun uid=1006 gid=1006
# mail seth 0

umask 22
CPLUS_INCLUDE_PATH=/usr/lib64/qt/include; export CPLUS_INCLUDE_PATH
MANPATH=/usr/local/man:/usr/man:/usr/lib64/adoptopenjdk12.0.2/man; export MANPATH

Removing jobs

You can remove pending jobs from your at queue using the atrm command and the job ID. If you don't know the job ID, use atq to view your queue first.

$ atq 

6   Fri Jan 01 00:00:00 2038  a  seth
$ atrm 6

$ atq

Scheduling with at

The at command is a little like a lesser cron system. It's useful for one-time command execution, and it's easy to use. If you've been using cron or sleep to offset the execution time of a command, take a look at at and batch.

[ Need to learn more about Linux system administration? Consider taking a Red Hat system administration course. ]

Topics:   Linux   Automation  
Author’s photo

Seth Kenlon

Seth Kenlon is a UNIX geek and free software enthusiast. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.