Knowledgebase: Cron
What
is cron?
Cron is a daemon that allows you to automate tasks by running programs
at regular intervals. You can define both the program that is to be run
as well as the time of day, day of week, week of month, month of year,
or any combination of these.
What
is a daemon?
A daemon is a server process that waits in the background until it is
needed.
When
during the day is the cron executed?
Commonly called "background running processes", cron is active
from the time the computer is turned on to when the computer is turned
off or cron is rebooted. Cron wakes up every minute, examines its list
of things to do, and checks to see if it should be run in the current
minute. If it has a program to run, it runs it. If it does not, it goes
back to sleep for another 59 seconds.
This list of things to do is called a cron table, or crontabs for short.
The crontabs is a schedule that is lists commands to perform, and the
date/time to run them.
How
do I know if I have cron access?
Because of the possible server burdens of enabling cron, many server administrators
disable access to cron. Of course, the easiest way to learn if you have
cron access is to ask your server administrator. If you'd rather not,
and you have telnet access, simply type: crontab -l
If you have cron access, you may see something similar to:
ares:/home3 $ crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (nothing installed on Mon Nov 29 23:06:56 1999)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp
$)
If you do not have crontab access, you will see an error message similar
to: You are not allowed to use this program (crontab)
See crontab(1) for more information
If you receive this error, you may wish to contact your server administrator
and ask permission to use cron.
How
do I use cron?
Okay… let's assume you have cron access and wish to run a program
that backs up your access log weekly. You've learned from your server
administrator that your access log is cleared out weekly on Mondays at
12:00am. You have a site statistics program that requires cron so it can
read the access log on a regular schedule and generate your statistics
for you. You've also downloaded and installed a file backup program (such
as BackMeUp) and now want to set up a crontab entry to run the program
every week.
We've found that one of the easiest ways to maintain multiple crontab
entries is to create a simple text file with each program to be run on
its own line and then to import the text file into your crontab. While
there is another method of editing crontabs (by using the crontab -e command),
we'll use the text file method. This way, you can also keep a backup copy
of your crontab entries on your local hard drive and re-upload it if necessary.
Crontab entries are split into 2 fields, the date/time field and the command
path. The date/time field is made up of 5 distinct components. Below is
a table of these components and valid assignments in the order they are
used in the crontab entry:
Component Acceptable Range
Minute of the Hour 0-59
Hour of the Day 0-23
Day of the Month 1-31
Month of the Year 1-12
Day of the Week 0-6
In the table above, the 0 in day of the week is Sunday; 6 is Saturday.
This first field is delimited with spaces, so in our example above, we
wanted to set the program to run at 12:00am each Monday. Here's what the
crontab entry will may like:
0 0 * * * 1 /home/perlarchive/www/cgi-bin/bmu.cgi
Let's match this line with the scheduling commands they represent:
0 0 * * 1 /path/to/command
Minute Hour Day Month Weekday Command
The asterisks (*) mean that the program should run in
all instances of the field. In the above example, we want the crontab
to be run each Sunday, which means that it should be allowed to run on
any day of the month (Sundays can fall on the 1st in one month, on the
5th in another, etc) and any month of the year.
The program path you see is the full server path to the program that's
to be run. It's extremely important to note that if the program to be
run requires any libraries (as BackMeUp) does, you should always use the
full server path to the libraries within the program that's to be run.
This is because cron runs from the root directory of your account. Since
it's essentially including the program that's to be run, it's running
the program from the root directory, and not from the directory your program
resides in. So if you're using any relative paths to libraries, your program
will not be able to find them in the root directory and the program will
fail.
What's extremely useful is that all output from the program, if any, will
be sent to the email address defined in your $ENV{' SERVER_ADMIN'} environment
variable (if defined). Therefore, you can receive confirmation of successful
runs or failures. Remember, all output from a Perl program (such as with
the print command) will automatically be directed to STDOUT, whose contents
will automatically be sent via email (if defined). You can also add a
line which will define (or temporarily change) the $ENV{'SERVER_ADMIN'}
variable. In your text file, add:
MAILTO=you@yourdomain.com
Let's get back on track. After you've completed your text file, save it.
In our example, let's call it cronentries.txt. Now type:crontab cronentries.txt
That's it - the commands and timetable you've entered in your text file
have been imported into the crontab. To double-check, type:
crontab -l
Here, you'll see the line(s) you entered in your cronentries.txt.
How
do I delete a cron tab?
Easy. Simply telnet to your account and type: crontab -r
That's all there is to it.
Why
won't my Cron job work?
One of the most common reasons a cron won't run on our servers is that
the first numeric entry does not have two decimal places.
This will not run:
5,20,35,50 * * * * /home/$user/$domain-www/somename.cgi
This will run the cron every 15 minutes:
05,20,35,50 * * * * /home/$user/$domain-www/somename.cgi
--->Note that you need double-digits if there is no * or /
This will run the cron 4 times a day - midnight, 6AM, Noon, and 6PM:
* 00,06,12,18 * * * /home/$user/$domain-www/somename.cgi
Explanation of field values:
* * * * * /home/$user/$domain-www/filename.php3
1 2 3 4 5 path to script
1 -- minute 00 - 59
2 -- hour of day 00 - 23(midnight is 00)
3 -- day of month 01 - 31
4 -- month of year 01 - 12
5 -- day of week 01 - 07
© 2003 Burningbulb.net |