Skip to content

~/avetyan.com/cheatsheets

Cheat sheets

cron cheat sheet

Schedule jobs with cron: the five-field syntax, special strings, where crontabs live, the environment trap, logging, locking and one-off jobs with at.

3 min read


Syntax

text
┌───────── 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 syntaxMeaning

Examples

ScheduleExpression

When both day of month and day of week are set, the job runs when either matches, not both.

Special strings

StringSame as

Crontab files

bash
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-parts
/etc/cron.d/backup
SHELL=/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>&1

Files 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

TrapFix

Don’t overlap

bash
*/5 * * * * flock -n /run/sync.lock /usr/local/bin/sync.sh

flock -n skips the run if the previous one still holds the lock.

Logs

bash
grep CRON /var/log/syslog               # Debian/Ubuntu
journalctl -u cron -u crond --since today

One-off jobs

bash
echo '/usr/local/bin/rotate-keys.sh' | at 02:00 tomorrow
at now + 30 minutes < job.sh
atq                                     # queued jobs
atrm 4

Keep 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…