Tuesday, May 17, 2011

CRON Jobs


To edit cron jobs
$ crontab -e
You may want to put in the following header
#MINUTE(0-59) HOUR(0-23) DAYOFMONTH(1-31) MONTHOFYEAR(1-12) DAYOFWEEK(0-6) Note 0=Sun and 7=Sun
#
#14,15 10 * * 0   /usr/bin/somecommmand  >/dev/null 2>&1
The sample “commented out command” will run at 10:14 and 10:15 every Sunday.  There will
be no “mail” sent to the user because of the “>/dev/null 2>&1″ entry.
$ crontab -l
The above will list all cron jobs. Or if you’re root
$ crontab -l -u
$ crontab -e -u
Reference “man 5 crontab”:
The time and date fields are:
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 Sun, or use names)
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”.
Ranges can include “steps”, so “1-9/2″ is the same as “1,3,5,7,9″.
Note, you can run just every 5 minutes as follows:
*/5 * * * * /etc/mrtg/domrtg  >/dev/null 2>&1
To run jobs hourly,daily,weekly or monthly you can add shell scripts into the
appropriate directory:
/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.weekly/
/etc/cron.monthly/
Note that the above are pre-configured schedules set in “/etc/crontab”, so
if you want, you can change the schedule. Below is my /etc/crontab:
$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly