Linux Command Reference ยท Packages and Services
Linux crontab command
Reference for crontab: edit and list scheduled jobs, the five time fields, special strings like @daily, environment pitfalls, logging output and common mistakes.
crontab edits the table of scheduled jobs for a user. The cron daemon reads these tables every minute and runs each command whose time pattern matches. It is the traditional way to run backups, log cleanups, reports and other recurring work on Linux.
Each user has their own crontab, stored under /var/spool/cron/ (the exact folder varies) and edited only through the crontab command. System-wide jobs go in /etc/crontab and in the folders /etc/cron.d/, /etc/cron.daily/ and similar; these have an extra column naming the user.
crontab -e # edit your table in $EDITOR crontab -l # list it crontab -r # remove it entirely
Try it
There is no cron daemon in the sandbox, so jobs would never run. The examples use the pieces that do work offline: a sample crontab to parse, and a small function that checks whether a time field matches.
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
crontab [-u USER] FILE crontab [-u USER] [-e | -l | -r | -i]
A schedule line has five time fields, then the command:
minute hour day-of-month month day-of-week command 0-59 0-23 1-31 1-12 0-7 (0 and 7 = Sunday)
| Item | Description |
|---|---|
* |
Every value. |
, |
List: 1,15 is the 1st and 15th. |
- |
Range: 1-5 is Monday to Friday in the weekday field. |
/N |
Step: */10 is every 10th value, 0-30/10 steps inside a range. |
@reboot, @hourly, @daily, @weekly, @monthly, @yearly |
Shortcuts replacing the five fields. |
-e, -l, -r |
Edit, list, remove the crontab. |
-i |
Ask before removing with -r. |
-u USER |
Act on another user's table (root only). |
FILE |
Replace the whole table with the file's content (- reads from standard input). |
Returns: 0 on success, non-zero when the editor fails, the syntax is wrong or you are not allowed to use cron (/etc/cron.allow and /etc/cron.deny).
Usage notes
- Cron has a tiny environment:
PATHis usually/usr/bin:/bin, the shell is/bin/sh, and your profile is not loaded. Use absolute paths, or setPATH=andSHELL=at the top of the crontab. - Percent signs are special. In the command field, an unescaped
%becomes a newline. Writedate +\%F, or move the command into a script. - Day-of-month and day-of-week combine with OR when both are restricted:
0 0 1 * 1runs on the 1st and on every Monday. - Output becomes mail. Anything a job prints is mailed to the owner (or
MAILTO). Redirect it:cmd >> /var/log/job.log 2>&1, or setMAILTO="". crontab -rhas no confirmation and sits next to-eon the keyboard; take backups withcrontab -l > cron.bak, or use-i.- Time zone: jobs use the system's local time zone, so daylight saving changes can skip or repeat an hour-based job. Servers often run in UTC to avoid this.
- Overlapping runs: cron starts a new copy every time even if the last one is still running. Guard long jobs with
flock -n /tmp/job.lock cmd. - Missed jobs (machine off) are not run later by cron; use
anacronor a systemd timer when that matters. - Alternatives: systemd timers (
systemctl list-timers) give logging in the journal, dependencies and catch-up runs.
Related
systemctl,logger,logrotate,date- Lessons: Cron and scheduling, Environment