Lesson 9.6: Scheduling tasks with cron
The cron daemon and crontab files, the five time fields (minute, hour, day of month, month, day of week) with *, lists, ranges and steps, special strings like @daily, the system crontab and /etc/cron.daily, editing your own crontab with crontab -e, checking it with crontab -l, how cron reports a mistake, and where to see that jobs ran.
20 min
You will learn to
- Read the five time fields of a cron line
- Write schedules with *, lists, ranges and steps
- Find system jobs in /etc/crontab and /etc/cron.daily
- Add a job with crontab -e and list it with crontab -l
- Recognise and fix a rejected crontab
- Check in the journal that cron ran a job
Key terms
- cron
- The daemon that checks every minute whether any scheduled job is due and runs it.
- crontab
- A table of scheduled jobs, one per line. Also the command to manage your own: crontab -e edit, -l list, -r remove.
- The five time fields
- minute (0-59) · hour (0-23) · day of month (1-31) · month (1-12) · day of week (0-7, 0 and 7 are Sunday), then the command.
- * , - /
- * any value · 1,15 a list · 1-5 a range · */10 every 10th value.
- @ strings
- @reboot at startup · @hourly · @daily · @weekly · @monthly · @yearly.
- System crontabs
- /etc/crontab and /etc/cron.d/* have an extra user field. Scripts in /etc/cron.hourly, cron.daily, cron.weekly and cron.monthly run at those intervals.
- cron's environment
- cron jobs run with a minimal PATH and no terminal: use full paths, redirect output to a file, and test by hand first.
← Services with systemd and journalctl · All lessons