TLcrontab.doc COPYRIGHT 2003 Kirk D Bailey A part of the TinyList V:1.7.X suite Released under the GNU GDL August 2003. Researching CRON and CRONTAB, I found several useful tutorals on the subject; this document borrows from several of them, with additional comments by this author. Material for this article came from http://www.aota.net/Script_Installation_Tips/cronhelp.php4 and http://www.internetconnection.net/support/tech-cron.shtml and from studying the man entries inside of Critter, my server. ------------------------------------------------------------------------------- The cron daemon is a long running process that executes commands at specific dates and times. To schedule one-time only tasks with cron, use at or batch. For commands that need to be executed repeatedly (e.g. hourly, daily or weekly), use crontab, which has the following options: crontab filename Install filename as your crontab file. crontab -e Edit your crontab file. crontab -l Show your crontab file. crontab -r Remove your crontab file. MAILTO=user@domain.com Emails the output to the specified address. The crontab command creates a crontab file containing commands and how often cron should execute them. Each entry in a crontab file consists of six fields, specified in the following order: minute(s) hour(s) day(s) month(s) weekday(s) command(s) The fields are separated by spaces or tabs. The first five are integer patterns and the sixth is the command to be executed. The following table briefly describes each of the fields. Mi H Da Mo Wd DOTHISTHING Field Value Description minute 0-59 The exact minute that the command sequence executes. hour 0-23 The hour of the day that the command sequence executes. day 1-31 The day of the month that the command sequence executes. month 1-12 The month of the year that the command sequence executes. weekday 0-6 The day of the week that the command sequence executes. Sunday=0, Monday = 1, Tuesday = 2, and so forth. command Special The complete command sequence variable that is to be executed. Each of the patterns from the first five fields may either be an asterisk (*) ( a wildcard, meaning 'accept all legal values') or a list of elements separated by commas. An element is either a number or two numbers separated by a minus sign (meaning an inclusive range). Note that the specification of days may be made by two fields (day of the month and day of the week). If both are specified as a list of elements, both are followed. For example: MAILTO=user@domain.com 0 0 1,15 * 1 /big/dom/xdomain/cgi-bin/scriptname.cgi The cron daemon would run the program scriptname.cgi in the cgi-bin directory on the first and fifteenth of each month, as well as on every Monday. To specify days by only one field, the other field should be set to *. For example: MAILTO=user@domain.com 0 0 * * 1 /big/dom/xdomain/cgi-bin/scriptname.cgi The program would then only run on Mondays. If a cron job specified in your crontab entry produces any error messages when it runs, they will be reported to you via email to the declared address as shown in the example. You may create crontab files in notepad (being sure to upload them in ASCII) or you may create them within telnet by simply typing: mcedit cronfile.txt For more information, consult the man pages. man pages are the directions and tutorials available to you right at the command line from within telnet. Type any of the following lines to open the relevant tutorials: man 5 crontab {enter} man 1 crontab {enter} man cron {enter} man at {enter} man batch {enter} man 1 cron {enter} Note: Your crontab file must end with a line feed - in other words, make sure to press [Enter] after the last line in the file. ------------------------------------------------------------------------------- The crontab tool is used to create, edit, view and delete your Cron tables. The Cron tables are the files that contain the lists of jobs or scripts you want the server to run at certain times or events. To edit your Cron table, simply type crontab -e at your command prompt. If you haven't set up any tasks, your Cron table will be empty. Here is an example of a Cron table featuring one task: # 30 minute webcam update 30 * * * * /cam/webcam_upd.pl The hash mark preceding the first line signifies it as a comment. The actual command contained in this table is on line 2. Reading this line is pretty easy, here is a breakdown of what each phrase means: 30 - the first characters of the string signify minutes. You can use the numbers 0-59 to have an event occur at that minute of the hour. The command in this example, 30, signifies that the script will be run at 30 minutes of the hour. * - the first "*" represents the hour. You can choose to display the hours as 0-23, 3am (4PM) or 3:00am (4:00PM). * - the second "*"represents the date. You can display the day of the month using the numbers 1-31. * - the third "*" represents the month. You can display the month using the numbers 1-12, Jan (jan) and January (january). Notice the Capitalization! * - the fourth "*" represents the day of the week. You can display the month using 0-6 where Sunday = 0, Monday = 1, Tuesday = 2, etc. You can also use Mon (mon) or Monday (monday) here. AGAIN, notice the Capitalization! /cam/webcam_upd.pl - the last portion of the line specifies which command or script will be run at the time and date you've chosen. As mentioned above, Cron can be used for many things and this fictional script would work in conjunction with a webcam. Note: Because the hour, date, month and day of the week in this example are asterisks and therefore wild cards, the script will not be limited to running certain hours, days, months or dates. So basically, the script will be run 30 minutes past the hour, every day, regardless of the hour, day, month or date. Here are some other examples of Cron table entries: * */2 * * Mon,Fri /run_every_2_hours_onMF.pl The "*/2" specifies that this script will run every two hours on Mondays and Fridays. It could have also been written as * */2 * * 1,5 /var/cgi/weekend.cgi with Monday and Friday being represented by the "1" and "5" respectively. * 2am */5,1,31 * * /cron/run_every_5_days_and_1st_and_31st.pl This entry specifies this script will run every 5 days and also on the 1st and 31st on the month, if it has a 31st (*/5,1,31) at 2 in the morning (2am). In addition to the editing mode of crontab, accessed with -e, there are two other switches you can use. To simply display the contents of your Cron table, type crontab -l. To delete your Cron table, use crontab -r. so a crontab to run newaliases command would be: # run newaliases each 15 minutes: # Mi Ho DoM Mo DoW Command */15 * * * * newaliases Cool?
ODD#kdb/TLcron.shtml