Thursday, July 16, 2015

Crontab syntax

$ crontab -l
Display the current crontab on standard output.

$ crontab -r
Remove the current crontab.

$ crontab -e
Edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables(vi as default).
After you exit from the editor, the modified crontab will be installed automatically.


Syntax:
#  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)               
eg:  0       2          12             *                *                    /usr/bin/find

Note:  - Spaces to separate fields.
           - No space allowed between values for a field.

==============================================

Range: - two numbers separated with a hyphen.
             - range is inclusive.    
             - eg: 8-11 for an "hours" entry specifies execution at
               hours 8, 9, 10 and 11.

   : - a special range from first to last for a field.
       - eg:  * in Hour field : all the hours from 0 to 23.

Step : - used in conjunction with ranges.
           - following a range with "/" specifies skips of the number's value
             through the range.
           - eg: "0-23/2" : to specify command execution every other hour
                                  (equal to "0,2,4,6,8,10,12,14,16,18,20,22").
                   "*/2" : to say "every two hours"

List: - a set of numbers (or ranges) separated by commas.
         - eg: "1,2,5,9"  or  "0-4,8-12".

Names : - can be used for the "month" and "day of week" fields.
                - first three letters of the particular day or month
                  (case doesn't matter).
               - ranges or lists of names are NOT allowed.

==============================================

eg:
#  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)               
eg:  0       2          12             *                *             /usr/bin/find
eg:  0       2          12             Feb            *            ~/abc.sh   > a.log   2>&1
eg:  0       0-2,5-20/3             *                */3         ~/abc.sh   > a.log   2>&1
eg:  0-59/5  2          1-31/3    *               0-6/2       ~/abc.sh   > a.log   2>&1

No comments:

Post a Comment