Nov 28, 2012

Cron Job Basics - Crontab



crontab -e      Edit your crontab file, or create one if it doesn’t already exist.
crontab -l      Display your crontab file.
crontab -r      Remove your crontab file.
crontab -v      Display the last time you edited your crontab file. (This option is only available on a few systems.)


min    0-59
hour   0-23
day    1-31
mon    1-12
dow    0-6 (Sunday = 0, Saturday = 6) DOW => Day of Week


min hr day month Week
30 00 01 01,06,12 * –> 00:30 Hrs on 1st of Jan, June & Dec.
00 20 * 10 01-05 –> 8.00 PM every weekday (Mon-Fri) only in Oct
00 00 01,10,15 * * –> midnight on 1st ,10th & 15th of month
05,10 00 10 * 01 –> At 12.05,12.10 every Monday & on 10th of every month
05 00 01 01 * -> 00:05 Hrs on 1st of Januray of each year

The asterisk in the field indicates to cron that any day of the week is acceptable.
However, since an explicit month and day have been specified, the day of the week is effectively ignored.


How to create a cron job that will run on the last day of each month, considering the fact that a month could have 28, 29, 30 or 31 days ?

Ans:
Your script, however, can test for that and exit if it is not the last day.
So - you'd set it in cron to run on the 28, 29, 30 and 31. Your script decides if it really is the last day and exits if it is not.
Or you can put each month into crontab separately and just deal with February in your script.


min hr day month Week
30 20 28,29 02 * /usr/prabhath/run.pl -> 8.30 PM every 28th of Feb
30 20 30 04,06,09,11 * /usr/prabhath/run.pl -> 8.30 PM every 30th of Months having 30 days
30 20 31 01,03,05,07,08,10,12 * /usr/prabhath/run.pl -> 8.30 PM every 31th of Months having 31 days

More Examples:
################
min hr day month Week User Command
01 * * * * root echo "This command is run at one min past every hour"
17 08 * * * root echo "This command is run daily at 8:17 am"
17 20 * * * root echo "This command is run daily at 8:17 pm"
00 04 * * 0 root echo "This command is run at 4 am every Sunday"
42 04 01 * * root echo "This command is run 4:42 am every 1st of the month"
01 * 19 07 * root echo "This command is run hourly on the 19th of July"


Example:
############
min hr day month Week User Command
* 12 16 * Mon root cmd

Will run cmd at midday every Monday and every 16th, and will produce the
same result as both of these entries put together would:
* 12 16 * * root cmd
* 12 * * Mon root cmd

Example:
############

min hr day month Week User Command
59 11 * * 1,2,3,4,5 root backup.sh
equals to
59 11 * * 1-5 root backup.sh


Step Values:
#################
Cron also supports 'step' values.
*/2 in the dom field would mean the command runs every two days and likewise,
*/5 in the hours field would mean the command runs every 5 hours.

Example:
############

min hr day month Week User Command
* 12 10-16/2 * * root backup.sh
is the same as:
* 12 10,12,14,16 * * root backup.sh

E.g.,

min hr day month Week User Command
*/15 9-17 * * * root connection.test
Will run connection.test every 15 mins between the hours or 9am and 5pm


min hr day month Week User Command
* 12 1-15,17,20-25 * * root cmd
Will run cmd every midday between the 1st and the 15th as well as the 20th and 25th (inclusive) and also on the 17th of every month.


O/P from Cron:
###################

min hr day month Week User Command
*/15 9-17 * * * root connection.test | mail -s "Subject" prabhathkota@gmail.com

*/15 9-17 * * * root connection.test >/dev/null 2>&1 #It won't send mail to anyone

*/15 9-17 * * * root connection.test > log.file

*/15 9-17 * * * root connection.test > log.file 2>&1

*/15 9-17 * * * root connection.test > mail -s "logfile for cmd" <log.file

*/15 9-17 * * * root connection.test >


Disable Email in Cronjob
#########################
By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .
>/dev/null 2>&1

No comments:

Post a Comment