Linux Command Reference ยท Processes and Signals
Linux cron and at commands
Reference for scheduling on Linux: cron expressions, special strings, environment and output rules, and at for one-time jobs. Includes a runnable schedule matcher.
Linux has two classic schedulers. cron runs jobs repeatedly on a calendar pattern ("every weekday at 02:30"); at runs a job once, at a time you name ("tomorrow at noon"). Both are daemons: they keep running in the background, wake up regularly, and start jobs on behalf of users with a very small, clean environment.
The scheduling daemons and the crontab and at programs do not exist in our sandbox, so the commands in this page are shown as read-only text. The live examples below run the one part you can test anywhere: how a cron expression is read and when it matches. For editing a table of jobs see crontab and /etc/crontab.
# minute hour day-of-month month day-of-week command 30 2 * * 1-5 /usr/local/bin/backup.sh echo 'tar -czf /tmp/docs.tgz ~/docs' | at 23:00 # once, tonight
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
A cron line has five time fields and a command:
MINUTE HOUR DAY-OF-MONTH MONTH DAY-OF-WEEK COMMAND
| Field | Allowed values |
|---|---|
| minute | 0-59 |
| hour | 0-23 |
| day of month | 1-31 |
| month | 1-12 (or names such as jan) |
| day of week | 0-7 (0 and 7 are Sunday; or names such as mon) |
Inside a field: * means every value, a,b a list, a-b a range, */n every n-th value, and a-b/n every n-th value of a range. Most crons also accept shortcuts in place of the five fields: @reboot, @hourly, @daily (or @midnight), @weekly and @monthly, @yearly (or @annually).
The at family:
| Command | Description |
|---|---|
at TIME |
Read commands from standard input and run them once at TIME (at 17:30, at now + 2 hours, at midnight tomorrow). |
atq |
List pending jobs. |
atrm N |
Remove job number N. |
batch |
Run when the system load is low. |
at -f FILE TIME |
Take the commands from FILE. |
Edge cases: if both day-of-month and day-of-week are restricted (neither is *), the job runs when either matches, not both: 0 0 13 * 5 fires on every 13th and on every Friday.
Usage notes
- A minimal environment. cron starts jobs with a tiny
PATH(often/usr/bin:/bin), no login files andSHELL=/bin/sh. Use absolute paths, or setPATH=andSHELL=at the top of the crontab. %is special. In a crontab command an unescaped%is turned into a newline (the rest becomes standard input). Writedate +\%F, notdate +%F.- Output becomes mail. Anything a job prints is mailed to the owner (
MAILTO=) when a mail system exists, and otherwise lost. Redirect explicitly:>> /var/log/job.log 2>&1. - Time zones and DST. Jobs use the system's local time zone. A job scheduled in the hour skipped or repeated by a daylight-saving change may run twice or not at all; servers often use UTC to avoid this.
- Jobs do not wait for each other. If a job runs longer than its interval, a second copy starts. Guard with
flockor a lock file (see Cron and Scheduling). - Missed runs. cron does not catch up after the machine was off;
anacronor a systemd timer withPersistent=truedoes. See Timer units. @rebootruns once when the cron daemon starts, not necessarily when the machine boots.atand the shell.atruns jobs with/bin/shand remembers the environment and working directory from when you submitted the job, unlike cron.- Test the command first by running it with
env -i HOME="$HOME" PATH=/usr/bin:/bin sh -c '...'; most "works by hand, fails in cron" bugs are environment differences. Seeenv.
Related
crontab,/etc/crontab, Timer units,sleep,nohup- Lessons: Cron and Scheduling, Jobs and Background