Cron

From The World according to Vissie
Jump to navigation Jump to search

Howto use cron on Debian


export EDITOR=/usr/bin/emacs
crontab -e
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

To run /path/to/command five minutes after midnight, every day, enter:

5 0 * * * /path/to/command

Run /path/to/script.sh at 2:15pm on the first of every month, enter:

15 14 1 * * /path/to/script.sh

Run /scripts/phpscript.php at 10 pm on weekdays, enter:

0 22 * * 1-5 /scripts/phpscript.php

Run /root/scripts/perl/perlscript.pl at 23 minutes after midnight, 2am, 4am …, everyday, enter:

23 0-23/2 * * * /root/scripts/perl/perlscript.


How do I use operators?

An operator allows you to specifying multiple values in a field. There are three operators:

The asterisk (*) 
This operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month.
The comma (,) 
This operator specifies a list of values, for example: “1,5,10,15,20, 25”.
The dash (-) 
This operator specifies a range of values, for example: “5-15” days , which is equivalent to typing “5,6,7,8,9,….,13,14,15” using the comma operator.
The separator (/) 
This operator specifies a step value, for example: “0-23/” can be used in the hours field to specify command execution every other hour. Steps are also permitted after an asterisk, so if you want to say every two hours, just use */2.

You can also use intervals, they are defined like this: */20 this example means every 20th and if in the minutes column this will be equivalent to 0,20,40 So to run a command every monday at 5:30 in afternoon:

30 17 * * 1 /path/to/command

or every 15 minutes

*/15 * * * * /path/to/command

Run this command every first Sunday of the month

0 19 1-7 * Sun [ "$(date '+\%a')" == "Sun" ] && /some/command