Linux Command Reference ยท Special Files and Directories
The /etc/crontab file
Reference for /etc/crontab and cron.d: the five time fields, the extra user field, special strings, environment variables and how it differs from per-user crontabs.
/etc/crontab is the system-wide schedule read by the cron daemon. It looks like a user crontab with one difference: a sixth field names the user who runs each command. The same format is used by files in /etc/cron.d/, which is where packages and administrators should put their own jobs instead of editing /etc/crontab. Per-user schedules, edited with crontab -e, have no user field.
Cron runs jobs with a tiny environment and /bin/sh, no terminal and a short PATH. Most "it works in my shell but not in cron" problems come from that.
# m h dom mon dow user command 30 2 * * * root /usr/local/bin/backup.sh
Try it
The sandbox has no cron daemon (and no /etc/crontab), so these examples explain and test the time fields in Bash.
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
Each job line has six leading parts, then the command (the rest of the line):
| Field | Allowed values |
|---|---|
| minute | 0-59 |
| hour | 0-23 |
| day of month | 1-31 |
| month | 1-12 or jan-dec |
| day of week | 0-7 (0 and 7 are Sunday) or sun-sat |
| user | A login name; the command runs as that user. Only in /etc/crontab and /etc/cron.d/. |
In each time field: * any value, a,b list, a-b range, */n or a-b/n step. The strings @reboot, @hourly, @daily, @weekly, @monthly and @yearly replace the five time fields. Lines of the form NAME=value set variables such as SHELL, PATH, MAILTO and HOME for the jobs that follow.
Edge cases: if both day-of-month and day-of-week are restricted (not *), the job runs when either matches, not both. A % in the command means a newline unless escaped as \%, which breaks date +%F unless you write date +\%F. Variable references are not expanded in the environment lines.
Usage notes
- Prefer
/etc/cron.d/NAMEover editing/etc/crontab. File names may contain only letters, digits, underscores and hyphens (no dots on Debian), must be owned by root and not writable by others. cron.hourly,cron.daily,cron.weekly,cron.monthlydirectories hold plain executable scripts run byrun-parts; they need no schedule line and no extension.- Use absolute paths and set
PATH, because the default is only/usr/bin:/bin. - Output is mailed to
MAILTO(or the owner); with no mail system it vanishes. Redirect to a log:>> /var/log/job.log 2>&1. - Time zone: cron uses the system zone; a daylight-saving change can skip or repeat a job set between 01:00 and 03:00.
- Overlap: cron does not stop a job that is still running. Guard long jobs with
flock -n. - Modern alternative: systemd timers give logging, dependencies and randomized delay; see
systemctl. - Syntax check: there is no dry run, so test the command by hand as the target user first.
Related
systemctl,nohup,date,env- Lessons: Cron and Scheduling, Environment