The crontab File Entry Format

A crontab file contains entries for each cron job. Entries are separated by newline characters. Each crontab file entry contains six fields separated by spaces or tabs in the following form:

# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
    0        2          12             *               0,6           ls -l > /dev/null

Sample commands:


/sbin/ping -c 1 192.168.0.1 &> /dev/null  
/sbin/ping -c 192.168.0.1; ls -la >> /var/log/cronrun
cd /path/to/the/working/dir/; /sbin/ping -c 1 192.168.0.1 &> /dev/null 

A field may be an asterisk (*), which always stands for "first-last".

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an "hours" entry specifies execution at hours 8, 9, 10 and 11.

Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: "1,2,5,9", "0-4,8-12".

Step values can be used in conjunction with ranges. Following a range with "/" specifies skips of the number's value through the range. For example, "0-23/2" can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is "0,2,4,6,8,10,12,14,16,18,20,22"). Steps are also permitted after an asterisk, so if you want to say "every two hours", just use "*/2".

Names can also be used for the "month" and "day of week" fields. Use the first three letters of the particular day or month (case doesn't matter). Ranges or lists of names are not allowed.


Special strings

Cron also offers some special strings:



  • string
    meaning

    @reboot
    Run once, at startup.

    @yearly
    Run once a year, "0 0 1 1 *".

    @annually
    (same as @yearly)

    @monthly
    Run once a month, "0 0 1 * *".

    @weekly
    Run once a week, "0 0 * * 0".

    @daily
    Run once a day, "0 0 * * *".

    @midnight
    (same as @daily)

    @hourly
    Run once an hour, "0 * * * *".
If you encounter errors from cron job, you should get email from crond. Mots system will send unhandled cron job output by email to root or the corresponding user.

Use:

 $ mailx


More control of crontab command

Note that the day of a command's execution can be specified by two fields: day of month, and day of week. If both fields are restricted (in other words, they aren't *), the command will be run when either field matches the current time. For example, "38 8 1,15 * 5" would cause a command to be run at 8:38 am on the 1st and 15th of each month, plus every Friday.

You can put the condition into the actual crontab command (the percent-syntax needs to be escaped):

0   12  1-7 *   *   [[ "$(date '+\%a')" = "Mon" ]] && echo "It's Monday"
Or

0   12  1-7 *   *    if [ "$(date +\%_d)" -le 7 ]; then echo "It's Monday"; fi
The "[[...]]" is a shorthand for IF in bash. The "&&" Ensures the command followed is only Executed if the condition evaluates to TRUE.


See also:

How to redirect both stderr and stdout to file in Bash

Featured Post

Updated Coupons / promo codes for Google Apps for Work / Business

To redeem: 1. Sign up for Google Apps   2. Go to your billing settings  3. Choose your payment plan  4. Enter your promo code ...