Using cron
When you're on a Unix/Linux server and you want server
software to launch at certain times and dates, you use
cron. Programs can be launched as often as every minute,
as seldom as once every year, or any recurring period
between those two extremes.
cron is a computer daemon, which is a program that
sits idly in the background until it is invoked or some
condition occurs that causes it to perform its task. The
cron program is a Unix/Linux clock daemon that executes
commands at specified dates and times according to
instructions in a crontab file.
I will show you how to make a crontab file.
A crontab file is a list of commands, one per line, that
are to be executed at a specified time. The date and time
come first in the crontab line, then the command to be
executed.
Whether you set up cron via a server web page interface,
cPanel for example, or set up cron via telnet/SSH with your
server's command line, you'll need to know how to create
the date/time and the command.
How To Create the Date/Time
The date/time part of the crontab line has six segments, a
space between each segment. Every segment contains a number
and/or one or more symbols.
The first segment represents the minute, the second
represents the hour, then the day of the month, the
month itself, and the day of the week.
At the exact moment all segments are true, that's when the
command runs (the command that follows the date/time).
A number is literal. An asterisk character is a wildcard.
To cause a command to execute every hour, every day, every
month, any day of the week, always at 3 minutes after the
hour, use this date/time:
3 * * * *
To have it execute only on Mondays at 3 minutes after the
hour, use this date/time:
3 * * * 1
Days of the week are represented by the numbers 0 through 6,
with 0 being Sunday and 6 being Saturday.
Months are represented by the numbers 1 through 12, with
1 being January.
Minutes are represented by numbers 0 through 59, hours by
numbers 0 through 23, and days of the month by numbers
1 through 31.
Another symbol that can be used is the slash character "/"
to represent a division.
To cause a command to execute whenever the hour is evenly
divisible by 4, every day, every month, any day of the week,
always at 3 minutes after the hour, use this date/time:
3 */4 * * *
The above date/time will cause a command to execute at 0:03,
4:03, 8:03, 12:03, 16:03, and 20:03, every day, month, and
day of the week.
To cause a command to execute every minute whenever the day
is Friday, the 13th, use this date/time:
* * 13 * 5
Another symbol that can be used is the hyphen character "-"
to represent a range.
To cause a command to execute every minute whenever the day
is Friday, the 13th, and when the months is anywhere from
February through July, inclusive, use this date/time:
* * 13 2-7 5
Another symbol that can be used is the comma character ","
to separate a list of numbers.
To cause a command to execute every minute whenever the
day is Friday, the 13th, and when the month is February,
October, or November, use this date/time:
* * 13 2,10,11 5
Remember that the only spaces in the date/time are the ones
that separate the segments. The segments themselves may
contain no spaces.
With the above, you can make pretty much any schedule you
wish.
Here is an example that I'll let you puzzle out:
13,18 * 5,8,10-15,25,31 * 0
(The answer is near the end of this article.)
How To Create the Command
On the line in the crontab file, following the five
date/time segments, is a space followed by the command to
execute. Unlike the date/time segments, the command may
contain spaces within itself.
The command is something the Unix/Linux operating system
would understand if it was typed directly via the operating
system command line. (That's why the command on the crontab
file line is called a "command," because it's what would
otherwise be typed on the command line.)
To invoke a system command, the command following the
date/time would be the command name.
To invoke a script, the command would be the location
of the interpreter, then a space, and then the location of
the script.
If the perl interpreter is at /usr/bin/perl, and the perl
script is located at /home/public_html/cgi-bin/script.cgi,
the command would be constructed like this:
/usr/bin/perl /home/public_html/cgi-bin/script.cgi
To run script.cgi every 15 minutes, the entire crontab line
would be:
*/15 * * * * /usr/bin/perl /home/public_html/cgi-bin/script.cgi
(The above most be all one line in the crontab file. This
is mentioned in case it wraps in the email you're reading.)
Creating a Crontab File with cPanel
With cPanel, and with some other server web page interface
control panels, setting up cron is a matter of clicking
some links until the right page is found.
On that page is a form with six fields for each schedule,
the first five fields being for the date/time segments and
the last field being for the command.
Fill in the date/time and command according to what you
learned above.
Some server web page interface control panels provide an
example of running a CGI file with a command that starts
with the word "GET". This should be okay in many instances.
For commands that launch scripts for sending an email to a
list, or other scripts that require some time to run, a GET
command might trigger a timeout if the script takes too
long, just like a browser might time out.
It is okay to use
GET http://example.com/cgi-bin/script.cgi
in most instances when the script will run for less than
a minute or two. Otherwise, use the "interpreter location
followed by script location" method of specifying the
command.
With cPanel, clicking the submit button creates the crontab
file.
Creating a Crontab File On the Server Command Line
Put a list of commands and their schedules in a plain text
file. Upload the file to your server.
The file can be named anything that makes sense to you.
For illustration purposes, however, let's assume the file
is named "mycron.txt"
Once mycron.txt has been uploaded, telnet or SSH to your
server.
At your server's command line, type
crontab mycron.txt
This will create the crontab file, a specially formatted
file generated from mycron.txt
If you want to verify that the crontab file indeed contains
your cron schedules, type
crontab -l
(the letter el, not the number 1). That will display a list
of the current cron schedules in the crontab file.
Now you know more about how to set up cron than most
people do.
Oh, yes, here is the answer to the puzzle presented
earlier in the article:
Whenever the minute matches 13 or 18,
and
any hour,
and
whenever the day of the month is 5, 8, 10, 11, 12, 13, 14, 15, 25, or 31,
and
any month,
and
whenever the day of the week is a Sunday.
In other words, whenever Sunday falls on any of the days
listed, the program will run at 13 and at 18 minutes after
every hour.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©2005 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.