Syntax
┌───────── 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)
│ │ │ │ │
* * * * * command| Field syntax | Meaning |
|---|---|
* | Every value |
5 | Exactly 5 |
1-5 | Range |
1,15 | List |
*/10 | Every 10th |
0-30/10 | 0, 10, 20, 30 |
Examples
| Schedule | Expression |
|---|---|
| Every 5 minutes | */5 * * * * |
| Every hour on the hour | 0 * * * * |
| Daily at 03:30 | 30 3 * * * |
| Weekdays at 09:00 | 0 9 * * 1-5 |
| Sundays at 02:00 | 0 2 * * 0 |
| First of the month at midnight | 0 0 1 * * |
| Every 15 minutes during work hours | */15 9-18 * * 1-5 |
When both day of month and day of week are set, the job runs when either matches, not both.
Special strings
| String | Same as |
|---|---|
@reboot | Once at startup |
@hourly | 0 * * * * |
@daily | 0 0 * * * |
@weekly | 0 0 * * 0 |
@monthly | 0 0 1 * * |
@yearly | 0 0 1 1 * |
Crontab files
crontab -e # your own
crontab -l
crontab -u alice -l # someone else's (root)
ls /etc/cron.d/ # system jobs, with a user field
ls /etc/cron.daily/ # scripts run by run-partsSHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[email protected]
30 3 * * * root /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1Files in /etc/cron.d/ have a user field; personal crontabs don’t. On Debian and Ubuntu, names with dots are ignored.
The crontab cheat sheet covers each of these places, run-parts naming rules and anacron in detail.
Environment traps
| Trap | Fix |
|---|---|
Minimal PATH | Set PATH= at the top or use full paths |
sh, not bash | Set SHELL=/bin/bash or call bash script.sh |
% means newline | Escape it: date +\%F |
No ~/.bashrc | Put what you need in the script |
| Output vanishes or gets mailed | Redirect: >> /var/log/job.log 2>&1 |
Don’t overlap
*/5 * * * * flock -n /run/sync.lock /usr/local/bin/sync.shflock -n skips the run if the previous one still holds the lock.
Logs
grep CRON /var/log/syslog # Debian/Ubuntu
journalctl -u cron -u crond --since todayOne-off jobs
echo '/usr/local/bin/rotate-keys.sh' | at 02:00 tomorrow
at now + 30 minutes < job.sh
atq # queued jobs
atrm 4Keep in mind
- Test the exact command as the cron user with a bare environment:
env -i /bin/sh -c 'your command'. - cron uses the server’s time zone; jobs between 02:00 and 03:00 can run twice or not at all on DST change days.
- For dependencies, retries or logging in the journal, a systemd timer is often the better tool.
Comments
Sign in with Google, GitHub or Facebook to comment.
Loading comments…
