Cron runs commands on a schedule: backups at night, cleanup of temporary files, reports on Mondays. The five-field syntax looks simple, but it is easy to get wrong — the job does not run, runs every minute, or twice as often as intended. Here are the format, examples for every case and the reasons a job "does not work".
Five 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) │ │ │ │ │ 30 2 * * * /usr/local/bin/backup.sh
Special characters:
*— any value;,— a list:1,15— the first and the fifteenth;-— a range:1-5— Monday to Friday;/— a step:*/15— every 15 units.
Examples
| Expression | When it runs |
|---|---|
*/5 * * * * | Every 5 minutes |
0 * * * * | At the start of every hour |
30 2 * * * | Every day at 02:30 |
0 9 * * 1-5 | Weekdays at 09:00 |
0 3 * * 0 | Sundays at 03:00 |
0 0 1 * * | Midnight on the first day of every month |
15 8,20 * * * | Twice a day: at 08:15 and 20:15 |
0 */6 * * * | Every 6 hours: at 0, 6, 12 and 18 |
@reboot | Once after the system boots |
Syntax traps
* 2 * * *is not "at two in the morning". An asterisk in the minute field means every minute from 02:00 to 02:59 — 60 runs. You need0 2 * * *.- Day of month and day of week combine with "or".
0 0 13 * 5fires on the 13th and on every Friday, not only on Friday the 13th. - A step counts from the start of the range.
*/45in the minute field means minutes 0 and 45 of every hour — gaps of 45 and 15 minutes, not exactly every 45 minutes. - The
%character in a command means a newline. Indate +%Fit must be escaped:date +\%F.
If in doubt, paste the expression into the cron decoder: it describes the schedule in words and shows the next runs in the time zone you need.
Why a job does not run
- A different environment. Cron runs commands with a minimal
PATH(usually/usr/bin:/bin) and without your variables. Use full paths to programs or setPATH=at the top of the crontab. - No newline at the end of the file. The last crontab line without a newline may be ignored.
- Permissions and owner. The script must be executable (
chmod +x), and files in/etc/cron.d/must be owned by root and not writable by group and others. Linux permissions are covered in the article on chmod. - The format of /etc/crontab and /etc/cron.d differs: the sixth field there is the user name. A line from your personal
crontab -ecopied there without a user name will not work. - Time zone. Cron uses system time; check
timedatectl.
To see errors, redirect output to a log: 30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1. The runs themselves are visible in journalctl -u cron (crond on RHEL) or in /var/log/syslog.
systemd timers instead of cron
Modern Linux systems increasingly use systemd timers instead of cron. They have advantages: a journal entry for every run in journalctl, running missed jobs after the server is switched on (Persistent=true), a random delay so hundreds of servers do not hit a service at once, and resource limits. The schedule is set with an OnCalendar line:
OnCalendar=*-*-* 02:30:00 # every day at 02:30 OnCalendar=Mon..Fri 09:00 # weekdays at 09:00 OnCalendar=*:0/15 # every 15 minutes
The Cron page translates a crontab expression into OnCalendar and gives ready .timer and .service files right away. If the job runs a service, you can give it a sandbox with systemd hardening.