cronjob tutorial – how to cronjob

  Asterisk

The Basic Format of a crontab/cronjob consists of 6 fields , placed on a single line and separated by spaces , formatted as follows

minute hour day month day-of-week command-line-to-execute
The acceptable values for each of the 6 fields are:
Field Range of values
minute 0-59
hour 0-23
day 1-31
month 1-12
day-of-week 0-7 (where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc)
command-line-to-execute the command to run along with the parameters to that command if any
The Fields have to be in that exact order ,with no empty or missing fields and everything must be placed on a single line
***********************************
Minute
***********************************
Minute” is a number from 0 to 59. “Hour” is a number from 0 to 23. They represent the time of the day in a 24-hour day format, so for example, if you want a certain command to run at 5.30 am, you will have to code it as:
30 5
If you want something run at 8 pm, it has to be coded as
0 20
since 2000 hours is 8 pm in the 24-hour time format.
***********************************
Day and Month
***********************************
Day” and “month” refer to dates. “Day” takes a value between 1 and 31, and “month”, as you may have already guessed, can take any value between 1 and 12. So if you want a command run on 5th January at 9.15 am, your schedule should begin with the following:
 
15 9 5 1
***********************************
Day of week
***********************************
Day-of-week” means basically which day you want your command to run. If you want your command to run on Sundays, use either 0 or 7 here. If you want it on Monday, use 1. (Note: if you are getting worried at this point how to combine all the various fields, some of which seem to contradict the other, don’t worry. We’re getting to that.)
 
The trick to scheduling things, say, once a day, or once in 2 hours or the like, is to use a wildcard character. A wildcard character is like the Joker in a pack of playing cards, that is, it is something that can represent any card in the pack. In a crontab file, the wildcard character “*” (the asterisk, without the quotes), represents every possible value for the field.
If you want a particular program to run, say, once every day at 10.45 am, the time portion of the cron schedule should read:
45 10 * * *
Here’s how to read the above line.
 
The first two fields “45 10” means that you want it to run at 10.45. The next field, the day field, is set to * (the asterisk character) to show that we’re talking about 10.45 every day, not just the 1st of the month (which would be “1”) or the 30th of the month (“30”) or some other number.
The month field is set to the asterisk as well. If we set some number in the month field, say “2”, we will be saying that we only want the command to run at 10.45 in the month of February (“2”). Since that’s not what we need, we put the asterisk to mean every month.
Similarly, the day-of-week field is set to the asterisk, because we want the command to run whether it’s Sunday (“0”) or Monday (“1”) or whatever day.

More Examples: An Hourly Schedule

Now if you want a job to run every hour on the hour, you will have to set the time component of the crontab line as follows:
0 * * * *
Can you see why? The “0” means at the top of the hour, that is, when the minute readout on a digital clock shows “00”. The asterisk in the hour field means every single hour. In other words, every hour, on the hour.

Alternate Hour or 3 Hourly Schedule

If you want something to run once every two hours, you will have to use the slash, “/”, character in your field. The slash character is the “step” character. In the case of a two hourly schedule, your time component of your cron file will read:
0 */2 * * *
The second field, “*/2”, means every alternate hour.
Similarly, if you want something to run every 3 hours, you can change that field to “*/3”, and so on.

Other Examples

If you want a particular command to run only at 8.00am on the 1st and 20th of every month, you should code the time as:
0 8 1,20 * *
The comma, “,”, means “and”. If you are confused by the above line, remember that spaces are the field separators, not commas.
What does the following schedule mean?
2 3 4,5 6 7
Decoded, the above line says at 3:02 am on the 4th and 5th of June (6) and on every Sunday (7), run your program.
There are other possibilities in the time fields, and I won’t go through all of them, since you already know enough to be able to construct whatever schedule you need.

 

How to specify the command line

The command-line-to-execute portion of the schedule is basically the command you want run at the specified time. For example, if you have a Perl script called “whatever.pl” that you want run every day at 11.30 am, your crontab schedule might read as follows:
30 11 * * * /your/directory/whatever.pl
Thanks to : http://www.thesitewizard.com/general/set-cron-job.shtml

LEAVE A COMMENT