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.
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
#!/bin/sh 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 test.sh $ ./test.sh
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
test.sh to run immediately, you can just use the keyword
$ at now -f test.sh 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
$ at now + 1 day -f test.sh warning: commands will be executed using /bin/sh job 2 at Tue Feb 25 01:27:00 2020
years keyword is undocumented, and
seconds is not supported. You can specify common times, too, such as
teatime (that's 16:00).
Simple times also work:
$ at 21:12 -f test.sh 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 test.sh 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 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
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
$ 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
b are reserved designations for
batch, and any queue with an uppercase letter is treated as a
batch job. Queues with names that alphabetically follow
b are run with increasing niceness.
Previewing your job
When you send a command or script to
at, your current working directory, environment (excluding
_), 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 #!/bin/sh # 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 KDE_MULTIHEAD=false; export KDE_MULTIHEAD [...]
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
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
sleep to offset the execution time of a command, take a look at
[ Need to learn more about Linux system administration? Consider taking a Red Hat system administration course. ]