Crontab
-
Crontab is like an alarm clock. It’s a task scheduler. It’s just a text file you create on your web server. The crontab file looks like this:
0 * * * * /etc/backup.cgi
0 0 * * * /etc/log.cgi -
Crontab file is located in /var/spool/cron. Do not edit the file directly. Instead, use crontab –e command.
-
The way the schedule looks is very cryptic but its really very simple. There are five fields to the schedule:
Minute(0-59) Hour(0-23) DayofMonth(1-31) MonthofYear(1-12) DayofWeek(0-7)
Note that ’0′ and ’7′ denote Sunday. Also note that the asterisk (*) will match any value. Instead of using numeric values, you may type ‘sunday’, ‘monday’, etc. (non-case sensitive).
-
The first command of our example:
0 * * * * /etc/backup.cgi
Means literally execute the script located at /etc/backup.cgi whenever the clock is equal to 0 minutes on any day, any hour, any day of month and any day of week. So the script is set to run once per hour exactly on the hour regardless of what day it is or what hour.
-
Now the second command:
0 0 * * * /etc/log.cgi
This crontab runs again whenever the internal clock hits Zero (0) Minutes, but instead of running once per minute it will only run once per hour. Why? Because we also set the Hour to zero so both the Minutes and Hour must be equal to zero before crontab will execute /etc/log.cgi. So this example runs once per day at midnight server time.
-
Now, you can get even more picky, final example lets setup a crontab to run only on Thursday at 2:55 PM.
- MIN = 55
- HOUR = 14
- DAYOFMONTH = *
- MONTHOFYEAR = *
- DAYOFWEEK = 4 (sun=0, mon=1, thu=2)
So our crontab entry would be
21 14 * * 2 /path/to/whatever/script.cgi
-
You can also use comma for multiple time in any entry:
5,10,15,20,25 10 * * * /command
-
Edit the crontab using command crontab –e It will open up a VI editor for you to edit your crontab. Remember to end every new crontab entry with a new line.
-
9By default, error message will be delivered to the crontab owner by email. You can define MAILTO error in the crontab (crontab –e) to replace default email.
MAILTO=email
-
If you wish to disable the email (and not output to a log file) then, at the end of each of the cron job lines you wish to not be notified on, place the command:
>/dev/null 2>&1
This essential writes the email out to nowhere (a trash bin of sorts), and thus solves the problem. Your final cron job line will look like this:
45 4 * * * rm /home/{username}/temp/* >/dev/null 2>&1
-
If you wish to disable the email (and output to a log file) then, at the end of each of the cron job lines you wish to not be notified on, place the command:
> {logfile path and name}
or
>> {logfile path and name}Special Note: One > means replace the current log with a new one, while two >> means append the current output to the end of the current log. This essential writes the email out to nowhere (a trash bin of sorts), and thus solves the problem. Your final cron job line will look like this:
45 4 * * * rm /home/{username}/temp/* > /home/{username}/cronlogs/clear_temp_dir.txt >/dev/null 2>&1
-
Examples of more complicated crontab entries:
30 9-17 * 1 sun,wed,sat echo `date` >> /date.file 2>&1
At half past the hour, between 9 and 5, for every day of January which is a Sunday, Wednesday or Saturday, append the date to the file date.file0 */2 * * * date
Every two hours at the top of the hour run the date command0 23-7/2,8 * * * date
Every two hours from 11p.m. to 7a.m., and at 8a.m.0 11 4 * mon-wed date
At 11:00 a.m. on the 4th and on every mon, tue, wed0 4 1 jan * date
4:00 a.m. on January 1st0 4 1 jan * date >>/var/log/messages 2>&1
Once an hour, all output appended to log filecommand | mail –s “Subject of mail” user
Send output of Command to user by email. You can substitute the user to any other email address.command | mutt –s “Subject of mail” –a “/file/path” user
Send email attachment to user using mutt utility.
For related reference you may also read An Introduction to Cron (by Interspire).
Thank you. Very helpful
links
Hello webmaster,
I would like to share with you a link, write to alarroste@mail.ru
i love this site.d