What cron is
Cron is the time-based job scheduler of Linux and other Unix-like systems. You tell it when to run a command and it does it for you, again and again. The time part is called a cron expression.
The five fields
* * * * * command_to_execute
- - - - -
| | | | |
| | | | +----- Day of the week (0 - 7) (Sunday is 0 or 7)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Some tools (for example Quartz and some cloud schedulers) use six or seven fields. They add seconds at the start and sometimes a year at the end. Standard Linux cron has five.
| Field | Range | Example |
|---|---|---|
| Minute | 0-59 | 0 is the start of the hour |
| Hour | 0-23 | 14 is 2 PM |
| Day of month | 1-31 | 15 is the 15th |
| Month | 1-12 | 7 is July |
| Day of week | 0-7 | 1 is Monday (0 and 7 are Sunday) |
Special characters
| Character | Meaning | Example |
|---|---|---|
* | Every value | * * * * * runs every minute |
, | A list of values | 1,15 * * * * runs at minute 1 and 15 |
- | A range | 1-5 * * * * runs at minutes 1 to 5 |
/ | A step | */5 * * * * runs every 5 minutes |
? | No specific value (Quartz style) | 0 0 1 * ? |
L | Last (Quartz style) | L in day of month is the last day |
W | Nearest weekday (Quartz style) | 15W is the weekday nearest the 15th |
?, L and W are not part of standard Linux cron.
Common examples
Every minute
* * * * * /path/to/script.sh

Every hour, at minute 0
0 * * * * /path/to/script.sh

Every day at midnight
0 0 * * * /path/to/script.sh

Every Sunday at midnight
0 0 * * 0 /path/to/script.sh

On the 1st of every month at midnight
0 0 1 * * /path/to/script.sh

Every 15 minutes
*/15 * * * * /path/to/script.sh

Weekdays at 6:30 AM
30 6 * * 1-5 /path/to/script.sh
Add the job
Open your crontab and add the line:
crontab -e
See what is scheduled with crontab -l.
Tip: Use full paths in cron jobs, and send the output to a log file, for example
>> /var/log/myjob.log 2>&1. Cron runs with a small environment.Quick recap
- Five fields: minute, hour, day of month, month, day of week.
*is every,,is a list,-is a range,/is a step.- Test your expression before you rely on it.