Cron Jobs and Schedules
Level 4 · Timed jobs and the firewall
How cron runs a command on a timer, and how to read the five fields of a line like 0 3 * * *. Why a job that works by hand can fail in cron.
Course contentsLesson 9 of 10
Level 1 · Typing commands
Level 2 · Files, users and permissions
Level 3 · Software and services
Level 4 · Timed jobs and the firewall
After this lesson you can
- Read a five-field cron line
- Add a job with crontab -e
- Say why cron jobs need full paths
11 mincovers 2 glossary entries
Worth reading first: systemd and Daemons
How to read 0 3 * * *
Cron is a daemon that runs commands on a timer. Each job is one line: five fields that say when, then the command. 0 3 * * * means at minute 0 of hour 3, every day, every month, every day of the week. In other words, every night at 3:00. You edit your list with crontab -e.
The five fields are, in order: minute, hour, day of the month, month, and day of the week. A star means any. A number means only that one. 1-5 in the last field means Monday to Friday. */15 in the first field means every 15 minutes.
So 30 9 * * 1-5 runs at 9:30 on weekdays. 0 0 1 * * runs at midnight on the first of each month. The times follow the server's own clock, and that clock may be set to a time zone that is not yours.
Cron runs the job with a very short PATH, and without the values your login window sets. A command that works when you type it can fail in cron because it cannot find a program. Use full paths in cron lines, such as /usr/bin/python3 rather than python3.
Cron does not show you what happened. Anything the job prints is lost, unless you send it to a file. Add >> /home/emma/backup.log 2>&1 to the end of the line, and every run leaves a record you can read.
A cron line is like a recurring alarm on a phone. Once it is set, it goes off on time whether anyone is watching or not. That is the point, and also the danger.
A common mistake is to forget the server's time zone. Many servers keep world time, which is one or two hours behind Berlin. A job set for 0 3 then runs at 4 or 5 a.m. in Berlin. Check with the date command before you pick the hour.
$ crontab -l
0 3 * * * /home/emma/backup.sh >> /home/emma/backup.log 2>&1
*/15 * * * * /usr/bin/curl -s https://emmas-shop.com/healthWorked example
Lucy's backup never ran
The words for this lesson2
Check yourself
When does the cron line 30 9 * * 1-5 run?
Finished this lesson?
Marking it complete also marks its 2 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.