What it does
Each user can have a crontab: lines with 5 time fields (minute, hour, day of month, month, day of week) and a command. crontab -e opens it in your editor and installs it when you save; crond checks the lists every minute.
root can manage the crontab of any user with -u. The files are stored in /var/spool/cron/USER — do not edit them directly. Who may use crontab is set in /etc/cron.allow and /etc/cron.deny.
How to write it
crontab [-u USER] FILE
crontab [-u USER] -e | -l | -r [-i]
crontab -T FILEMain parts
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 = Sunday) or sun–sat.
*,-/- Any value, list (1,15), range (1-5), step (*/10).
@reboot@hourly@daily@weekly@monthly@yearly- Shortcuts instead of the 5 fields.
Options 11
Most used
Nothing matches this filter.
Examples 12
$ run as a normal user# run as root (or with sudo)
1Edit your crontab
crontab -eOpens vi (or $EDITOR). Save and quit to install it.
2List your jobs
crontab -l */10 * * * * /usr/bin/df -h / >> /var/tmp/disk.logNo crontab: "no crontab for USER".
3Manage another user's jobs
crontab -e -u lisa crontab -l -u lisaThe RHCSA way for tasks "user X must run …".
4Weekdays at 14:23
crontab -l | grep logger 23 14 * * 1-5 logger "anna was here"Monday to Friday.
5Install a crontab from a file
crontab mycron.txtReplaces the whole list. Good for versioned crontabs in git.
6Add one line without an editor (scripts)
(crontab -l 2>/dev/null; echo '0 2 * * * /usr/local/bin/backup.sh') | crontab -Keeps the existing lines.
7Back up all crontabs
tar -czf /root/crontabs.tgz /var/spool/cron /etc/cron.d /etc/crontabBefore you rebuild a server.
8Remove a crontab with a question
crontab -ri crontab: really delete student's crontab? yWithout -i, -r removes everything at once.
9Run a job at every boot
crontab -l | grep reboot @reboot /usr/local/bin/start-tunnel.shA systemd unit is often the better choice.
10Log the output of a job
crontab -l 0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1Without it, output goes to local mail.
11Find all cron jobs on a system (security)
Attackers like cron for persistence; check for unknown jobs.
12Check the cron log
Shows when jobs ran and for which user.
Nothing matches this filter.
Watch out
crontab -ris next to-eon the keyboard and deletes everything without asking. Use-ri.- cron uses a small PATH: write full paths.
%in a crontab line means a newline; write\%(for example indate +\%F).
Files
/var/spool/cron/USER- The crontabs of users.
/etc/crontab, /etc/cron.d/- System crontabs (with a user field).
/etc/cron.allow, /etc/cron.deny- Who may use crontab.
/var/log/cron- Log of crond.
Exit codes
0 :: Done. 1 :: Error (not allowed, syntax error, no crontab).