![]() |
|||||||||||||||||||||||||||
| This website is not maintained anymore. Please visit www.nerdgirl.dkinstead | |||||||||||||||||||||||||||
|
This page was last modified: July 27 2010 13:22:13 | ||||||||||||||||||||||||||
Crontab - how to make cronjobsIt's easy to make your server carry out tasks by itself at a specific time. This is done with crontab (1). Command optionsThe crontab file is located in /etc/crontab. But never edit the crontab file directly - always use the contab command. It is always a bad idea to change system files and /etc/crontab is a system file. It could therefore in theory, change during a future update of FreeBSD.
The u- (user) option can be used in conjuction with all of the above to list/edit/remove crontab for a specific user. This option is not necessary if you are logged in as that user. Adding a job to cronThe format of crontab is ... minute hour day month weekday who command
Minute - Minutes after the hour (0-59). I have a perlscript that fetches news from a newsgroup, and saves them in a database. I want this script to run every 10 minutes. First I tell crontab that I want to edit the crontab file: crontab -e crontab now opens the crontab file with my default editor. This line is then inserted: 0,10,20,30,40,50 * * * * /path/to/script/scriptname.pl When you save and exit, the system wil automatically update with the newly added job(s) The above relates to each field in crontab like this: Minute 0,10,20,30,40,50Hour * Day * Month * Weekday * Command /path/to/script/scriptname.pl Now, imagine what a cronjob running every 5 minutes would look like... not a pretty sight... but fortunately there is at shortcut: 0-59/5 * * * * /path/to/script/scriptname.pl Here is a few other examples:
30 0 * * * /path/to/scriptname.pl
30 0 * * * /path/to/scriptname.pl
0,10,50 9-15 * * * /path/to/scriptname.pl
0-59/10 * * * 1,3,5 /path/to/scripname.pl Each crontab is related to a user. If you are logged in as root when editing crontab, the jobs you add will be run as root. If you have a job that should be run as user joe, you must su to that user or use the -u option when adding the job with crontab -e As mentioned ealier, the system crontab is located in /etc/. Other users crontab files, is located in /var/cron/tabs/ Directing the output...Cron will by default mail any output to the owner of the job. But it is also possible to redirect output to a logfile, or maybe you do just want to trash it... Use >/dev/null 2>&1, if you don't want any output anywhere: 0-59/10 * * * * /path/to/script/scriptname.pl>/dev/null 2>&1 In this example, errors are logged to a file and anything else is thrown overboard:
0-59/10 * * * * /path/to/script/scriptname.pl We can also log errors to one file, and anything else to another file:
0-59/10 * * * * /path/to/script/scriptname.pl Remember to write it all in one line when editing crontab. .. or we can mail it anywhere we want...:
MAILTO="name@example.com" When specifying MAILTO in the crontab file, output from all the jobs in that file will be sent to that mail address, unless dev/null, a log file, or another mail address is specified. Trashing or directing output to a file is already described, and here is how to direct a specific job to an email address: 0-59/10 * * * * /path/to/script/scriptname.pl | mail -s "Subject of mail" user@domain.tld ... and remember to put it all on one line. For further informations on this subject, see crontab(1) and cron(8) of the man pages. |
|||||||||||||||||||||||||||