Linux Labs

Packages, services, and logs / Stage 11 of 13

Cron jobs and systemd timers

Not running Starting usually ~20 s
Leave the lesson

Step 2 of 8 · theory · 5 min

A cron schedule line

Cron schedules live in two places: users' personal tables and system files. The line inside them looks almost the same.

cron is a service that checks the clock against the schedules once a minute and starts the jobs that match. A schedule line is five time fields and a command:

Field Values
minute 0–59
hour 0–23
day of month 1–31
month 1–12
day of week 0–7, 0 and 7 are Sunday

* in a field means any value, */10 every tenth, 1-5 a range, 1,15 a list. 15 2 * * * is every day at 02:15, * * * * * every minute.

A personal table is the user's crontab: crontab -l prints it, crontab -e opens it in an editor (the first time it asks which one: 1 is nano). Its commands run as the table's owner. In the system files /etc/crontab and /etc/cron.d/* there is one more field between the time and the command: the name of the user to run as.

Check yourself 1 question

This line from /etc/cron.d was copied as is into a personal table with crontab -e. What happens at 02:15?

15 2 * * * student /usr/local/bin/backup.sh
  • crontab checks the time format and treats the word after it as the start of the command. A line with five fields and a command is valid to it.

  • In a personal table the command follows the five fields directly. cron runs student /usr/local/bin/backup.sh, and the shell looks for a program called student.

The user field exists in /etc/crontab and /etc/cron.d, while in a personal table the command starts in its place. cron throws the error away with the output, and the run shows up in sudo journalctl -u cron.

Key points

  • A cron line is five time fields and a command; * means "any".
  • A personal table is crontab -l and crontab -e; its commands run as its owner.
  • In /etc/crontab and /etc/cron.d the time is followed by a user name.
Ubuntu 24.04
Loading…