history 29 of 36
How to Create a Cron Job with crontab
The five crontab time fields and their allowed values, how to add jobs with crontab -e and list them with crontab -l, and worked schedule examples.
Written
From my Linux study notes, 2017. Some commands and versions here are old, so please reuse them carefully. Anything marked "added later", and the notes at the end, explain what has changed since.
Cron runs commands automatically on a schedule. Each user has their own list of jobs, called a crontab.
The original notes
Allowed values for each field
| Field | Allowed values |
|---|---|
| Minute | 0-59 |
| Hour | 0-23 |
| Day of month | 1-31 |
| Month | 1-12 (or names, see below) |
| Day of week | 0-7 (0 or 7 is Sunday, or use names) |
The order of the fields
Minute - Hour - Day of Month - Month - Day of Week
* * * * * Command goes hereA * means “every” value for that field, so * * * * * runs the command every minute.
How to find this table on your own system
info crontab | grep -A 6 -i 'allowed values'This searches the crontab documentation for the “allowed values” table and prints the six lines after the match. The table lives in the crontab file-format manual page, so if the command above prints nothing on your system, open that page directly:
man 5 crontabNotes for today
Each field explained
- Minute (0-59): the minute past the hour.
- Hour (0-23): 24-hour clock, so 0 is midnight and 14 is 2pm.
- Day of month (1-31).
- Month (1-12): you can also use the first three letters of the name, such as
janordec. - Day of week (0-7): 0 and 7 are both Sunday, 1 is Monday. Names such as
monorfrialso work.
Special characters you can use in any field:
*every value,a list, for example1,15(the 1st and 15th)-a range, for example1-5(Monday to Friday in the day-of-week field)/a step, for example*/15in the minute field (every 15 minutes)
Add, list and remove jobs
Edit your crontab (it opens in your default editor, often vi):
crontab -eAdd one job per line, save and quit. Cron picks up the change automatically; you do not need to restart anything.
List your current jobs:
crontab -lBe careful with crontab -r: it deletes your whole crontab without asking. The r key sits next to e, so it is easy to type by mistake.
Jobs that need root (for example changing system settings) belong in root’s crontab. Use sudo crontab -e, or crontab -e when logged in as root.
Examples
Run a backup script every day at 02:30:
30 2 * * * /home/user/backup.shRun a script every 15 minutes:
*/15 * * * * /home/user/check.shRun at 09:00 Monday to Friday:
0 9 * * 1-5 /home/user/report.shRun at midnight on the first day of every month:
0 0 1 * * /home/user/monthly.shThe script paths above are only examples; use the full path to your own script.
Check it worked
-
Run
crontab -lto confirm the line was saved. -
Make the job write to a log so you can see that it ran:
*/5 * * * * /home/user/check.sh >> /tmp/check.log 2>&1>>appends normal output to the file and2>&1sends errors to the same place.
Common mistakes
- Relying on your login environment. Cron runs with a very small
PATHand does not load your shell settings. Use full paths to commands and scripts, for example/usr/bin/phprather thanphp. Runwhich phpto find the full path. - Script not executable. Make it executable with
chmod +x script.sh, or call it through the interpreter, for example/bin/bash /home/user/backup.sh. - The
%character. In a crontab line,%is treated as a newline. Escape it as\%, which matters for commands such asdate +%Y-%m-%d. - Day of month and day of week together. If both fields are set (neither is
*), the job runs when either one matches, not only when both match. - Timezone. Cron uses the server’s local time. Check it with
date.