ag@linux:~$

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 here

A * means “every” value for that field, so * * * * * runs the command every minute.

How to find this table on your own system

Terminal window
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:

Terminal window
man 5 crontab

Notes for today

Each field explained

  1. Minute (0-59): the minute past the hour.
  2. Hour (0-23): 24-hour clock, so 0 is midnight and 14 is 2pm.
  3. Day of month (1-31).
  4. Month (1-12): you can also use the first three letters of the name, such as jan or dec.
  5. Day of week (0-7): 0 and 7 are both Sunday, 1 is Monday. Names such as mon or fri also work.

Special characters you can use in any field:

  • * every value
  • , a list, for example 1,15 (the 1st and 15th)
  • - a range, for example 1-5 (Monday to Friday in the day-of-week field)
  • / a step, for example */15 in the minute field (every 15 minutes)

Add, list and remove jobs

Edit your crontab (it opens in your default editor, often vi):

Terminal window
crontab -e

Add one job per line, save and quit. Cron picks up the change automatically; you do not need to restart anything.

List your current jobs:

Terminal window
crontab -l

Be 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.sh

Run a script every 15 minutes:

*/15 * * * * /home/user/check.sh

Run at 09:00 Monday to Friday:

0 9 * * 1-5 /home/user/report.sh

Run at midnight on the first day of every month:

0 0 1 * * /home/user/monthly.sh

The script paths above are only examples; use the full path to your own script.

Check it worked

  • Run crontab -l to 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 and 2>&1 sends errors to the same place.

Common mistakes

  • Relying on your login environment. Cron runs with a very small PATH and does not load your shell settings. Use full paths to commands and scripts, for example /usr/bin/php rather than php. Run which php to 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 as date +%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.