Cron Jobs Explained: Schedule Tasks on Linux
Your server should back itself up every weekday at 2:30am, while you sleep. On Linux, the tool for that is cron, and the tasks it runs are called cron jobs. This lesson shows how to write their schedules, install them, avoid the traps that make a job fail silently, and check that it ran.
What cron is
cron is a daemon: a service that runs in the background with no terminal, like those in lesson 12. Its unit is cron.service on Debian and Ubuntu and crond.service on Fedora and Red Hat. Once a minute it wakes up, reads its schedules and starts every command that is due.
The schedules live in a crontab, short for cron table: a text file with one job per line, and each user can have one. A cron job is one of those lines: when, then what to run. cron also looks for changed crontabs every minute, so you never restart it after an edit.
The five time fields
A line starts with five fields separated by spaces: minute, hour, day of month, month and day of week. Everything after them is the command, which cron runs with /bin/sh. The diagram reads a backup line field by field.
The end of the line is redirection from lesson 8, appending output and errors to the log. The job runs as anita, so she must be able to write that file: an admin creates it once with sudo touch and hands it to her with sudo chown anita (lesson 6).
Four special characters build patterns. * means every value. A comma makes a list, a dash a range, and a slash a step, every nth value. In day of week, 0 and 7 both mean Sunday, and names such as mon work too. Examples:
| Schedule | When it runs |
|---|---|
*/5 * * * * | every 5 minutes |
0,30 9-17 * * * | on the hour and half hour, 09:00 to 17:30 |
0 9 * * 1 | Mondays at 09:00 |
@reboot | once, when cron starts at boot |
A step counts from the start of the range, not from when you saved the line: */15 in the minute field means minutes 0, 15, 30 and 45. In the timeline, notice where the first run lands.
Watch one rule: if both day of month and day of week are set, the job runs when either matches. 0 9 1 * 1 runs on the 1st of every month and on every Monday, not only on a Monday the 1st.
Editing crontabs: crontab -e and /etc/cron.d
On Debian and Ubuntu, cron keeps each user's crontab in /var/spool/cron/crontabs, but you never edit those files directly. The crontab command does it for you and checks your lines:
crontab -eopens your crontab in the editor named by VISUAL or EDITOR: environment variables, variables (lesson 17) that the shell passes to programs it starts. Without either, it uses the system's default editor;EDITOR=nano crontab -epicks nano for one edit. When you save and leave, it printscrontab: installing new crontab. It refuses a broken line and asks whether to retry the edit.crontab -llists your crontab, or printsno crontab for anitaif you have none.crontab -rdeletes your whole crontab, without asking. The r key sits next to e, so type carefully.sudo crontab -u anita -llists another user's crontab.
Jobs that belong to the system rather than to one person go in /etc/crontab or in a file in /etc/cron.d/. These have one extra field between the schedule and the command: the user to run as. The backup job as a system file looks like this:
# /etc/cron.d/backup: weekday backups, run as anita
30 2 * * 1-5 anita /home/anita/backup.sh >> /var/log/backup.log 2>&1
On Debian and Ubuntu, files in /etc/cron.d must be owned by root, and their names may contain only letters, digits, hyphens and underscores: backup runs, but backup.cron is ignored. Every line, the last one included, must end with a newline.
The usual traps
cron runs your command in a much smaller world than your terminal, so a job that works when you type it can fail:
- A minimal PATH. On Debian and Ubuntu, a job's PATH, the folders searched for a command's name (lesson 16), is only
/usr/bin:/bin. A program in/usr/local/binor/usr/sbinis then not found. - Relative paths. A job starts in your home folder, not in the script's folder, so a relative path such as
data/points somewhere else. Use absolute paths for every program and file; for programs, aPATH=line at the top of the crontab also works. - No terminal. Nobody can answer a question, so a command that asks for a password, such as sudo, fails. Your
~/.bashrcis not read either, so variables you set there are missing. - Percent signs. In a crontab line,
%ends the command and becomes a newline, sodate +%Fbreaks the line. Write\%, or keep such commands in the script. - The time zone. The schedule uses the server's time zone, often UTC on cloud servers, not yours.
dateortimedatectlshows it. - Lost output. cron mails whatever a job prints to its owner. Many servers have no mail system, or MTA (mail transfer agent), so Debian logs
No MTA installed, discarding outputand the messages are gone. Redirect both streams to a log, as in the backup line.
Did it run? Logs, and systemd timers
Look in two places. cron's own log records every job it starts: on Debian and Ubuntu, read it with journalctl from lesson 13, or in /var/log/syslog if rsyslog is installed; on Red Hat, it is /var/log/cron. Here grep keeps today's lines that mention CMD:
$ sudo journalctl -u cron --since today | grep CMD
Dec 02 02:30:01 web1 CRON[2345]: (anita) CMD (/home/anita/backup.sh >> /var/log/backup.log 2>&1)
The time, host and process ID are illustrative. CMD says that cron started the command as anita, and nothing about whether it worked: for that, read the job's own log, the file the line redirects to. When neither log shows anything, work down the flowchart.
Newer systems also have systemd timers: a .timer unit that starts the .service unit of the same name on a schedule such as OnCalendar=Mon..Fri 02:30. The job's output lands in the journal, systemctl list-timers shows the next run, and Persistent=true catches up on a run missed while the machine was off.
Check yourself
You save */10 * * * * at 14:03. When does the job first run?
At 14:10. A step counts from minute 0 of the hour, so the job fires at :00, :10, :20 and so on, whenever you saved it.
A job calls backup-tool, from /usr/local/bin. It works in your terminal, but the job's log says not found. Why?
cron's PATH is only /usr/bin:/bin, so the shell never looks in /usr/local/bin. Write /usr/local/bin/backup-tool in full, or add a PATH= line to the crontab.
journalctl shows a CMD line for your backup every night, but no backup file appears. What does that tell you?
cron and the schedule work, because cron started the command. The command itself failed, so read the job's own log, which 2>&1 fills with its errors.