How a cron expression works
A standard cron expression has five fields, separated by spaces:
┌───────── minute (0-59)
│ ┌─────── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌─── month (1-12 or JAN-DEC)
│ │ │ │ ┌─ day of week (0-7 or SUN-SAT; 0 and 7 are Sunday)
│ │ │ │ │
* * * * * command to runEach field takes a number, a list (1,15), a range (1-5), a step (*/10 or 0-30/5) or * for every value. 30 2 * * * runs at 02:30 every day; */5 * * * * every five minutes; 0 9 * * 1-5 at 09:00 on weekdays.
Shortcuts
Most cron implementations accept @hourly, @daily (or @midnight), @weekly, @monthly, @yearly (or @annually) and @reboot, which runs once when the cron daemon starts after a boot.
The day-of-month and day-of-week trap
When both day fields are restricted, cron runs the job when either matches, not both. 0 0 13 * 5 runs on every 13th of the month and on every Friday — not only on Friday the 13th. The next-runs list above follows that rule, so you can see it happen. To run only on Friday the 13th, schedule on the 13th and check the weekday inside the script.
Time zones
The system cron daemon uses the server's time zone, which on most cloud images is UTC. Check it with timedatectl. The next-runs list above is shown in your browser's time zone, so compare carefully when the two differ.
Editing the crontab
crontab -eedits the current user's crontab;sudo crontab -eedits root's.crontab -llists it.- Files in
/etc/cron.d/use the same format with an extra user field before the command.
Why a cron job does not run
It works in the shell but not from cron
Cron starts commands with a minimal environment: a short PATH, no shell profile, no nvm. Use absolute paths (/usr/bin/node, not node), or set PATH= at the top of the crontab.
Nothing in the logs
Without a redirect, cron mails the output to the user — usually to nowhere. End the line with >> /var/log/job.log 2>&1 so both output and errors are kept, as the generated line does.
% signs
In a crontab, % means a new line. Escape it as \% — a common surprise with date +%F.
Manage cron from Shipown
Shipown reads every cron job on a server — the user's, root's, /etc/cron.d and the periodic directories — and edits a crontab with the syntax checked and a backup written before it is installed. Its database backups schedule themselves the same way.
Frequently asked questions
What does */5 * * * * mean in cron?
Every five minutes: at minute 0, 5, 10 and so on of every hour, every day.
How do I run a cron job every day at a specific time?
Put the minute and the hour in the first two fields and leave the rest as asterisks. 30 2 * * * runs at 02:30 every day in the server time zone.
Why does my cron job run on the wrong day?
If both the day-of-month and day-of-week fields are set, cron runs when either one matches. Restrict only one of them, or check the other condition inside the script.
Which time zone does cron use?
The system cron daemon uses the server time zone, often UTC on cloud servers. Check it with timedatectl before choosing an hour.
Why does my script work manually but not in cron?
Cron runs with a minimal environment and a short PATH. Use absolute paths to every program, load any environment the script needs, and redirect output to a log file to see the error.