|
cron enables you to schedule things to be done automatically.
cron is the system scheduler for Unix. Using cron
, you can schedule events to occur daily, weekly,
monthly, hourly, or whenever. Any command or set of commands
you can run from a Telnet prompt can be run from cron
. For detailed information on cron , you can
Telnet to your server and type man 5 cron tab at
the command prompt. Much of the information in this section
is taken from the man (manual) page written by
Paul Vixie.
Each Virtual Server can
load its own cron job to execute scheduled tasks.
The most effective way to use cron is to load
the scheduled tasks into the cron daemon from
a file that you have created and stored on the Virtual Server. Although it is possible to manipulate cron
directly, loading cron jobs from pre-formatted
files will ensure that you have a copy of the file around
for editing and for archival purposes. A common place
to put such a cron file is in a directory called
cronfiles in your ~/etc directory.
Making the cronfiles
Directory
- Connect to your Virtual Server via Telnet.
- Type:
% cd ~/etc
% mkdir cronfiles
You can then store
the file(s) holding your cron information
in this directory. After you have made the cron
file, you need to load it into the cron program
(daemon).
Loading a File into
the cron Program
Change directory to where
the file is located on your Virtual Server.
% cd ~/etc/cronfiles
If you have placed a
cron file in the directory named my_cron_file,
load the file into the cron program by typing:
% crontab my_cron_file
A copy of the cron
file you created is in memory in the cron program.
To view cron 's copy in memory, you can call
the cron program with the -l (list) option:
% crontab -l
cron has other
command line options such as "edit" and "remove".
These commands will allow you to manipulate the information
that cron has in memory. For example, if you
wanted to add another event to the cron information,
you can use the crontab -e option:
% crontab -e
This will take the copy
of the entry that is stored in the cron programs
memory, and allow you to edit it. This is, however,
a less preferable option than changing the physical
file and re-loading it into cron , because the
changes are not physically stored anywhere accept in
cron 's memory.
% crontab -r
This removes the cron
entry you just loaded.
Note: If you created
a cron entry with crontab -e and your
run crontab -r, you will lose your cron entry
forever. This is a good reason to keep a physical copy
of your cron file and load it into memory.
Creating cron Files
In a cron file,
blank lines are ignored. Lines that have a pound sign
(#) as the first character are considered comments.
There are two types of cron entries: environment
variables and cron commands.
Environment Variables
Environment variables
have the form:
name = value
The spaces around the
equal sign are optional and any spaces in the "value"
will be included in the value being set. The value string
may be placed in quotes (either single or double) to
preserve leading or trailing spaces.
One environment variable
that can be set is the MAILTO variable. If MAILTO
is defined, any mail that is sent by cron , such
as error notifications, are sent to the address assigned
to the variable. If this value is not explicitly defined,
error mail messages will be sent to the Virtual Server's
administrator login name. For example, if your Virtual Server's administrator login name (i.e., Telnet login
name) were "judy", administrative e-mail from
the cron daemon would be sent to judy@yourcompany.com.
An example MAILTO entry might look like:
MAILTO=johndoe@yourcompany.com
If MAILTO is defined
as follows, no mail will be sent from cron :
MAILTO=""
Setting cron Commands
Each command entry in
a cron file is composed of a series of fields
that cron uses to determine what event to run
at a specific time and date. The first five fields (space
delimited) specify time and date information as follows:
|
CRON
Time and Date Fields |
|
Field |
Allowed
Values |
|
Minute |
0-59 |
|
Hour |
0-23 |
|
Day of Month |
0-31 |
|
Month |
0-12 (first three
letters of month names allowed) |
|
Day of Week |
0-7 (first three
letters of weekday names allowed) |
An asterisk may be used
as a wildcard meaning "first through last".
The asterisk is used when you want an event to occur
for every allowable value. For example, if you wanted
to schedule your log files to be purged on a monthly
basis you could place an asterisk in the Day of Month
field. As you might imagine, it would be unwise to put
an asterisk in the Minute field of the cron file
as it may cause too much of a load on your Virtual Server.
Ranges such as two numbers
separated with a hyphen ("-") are allowed.
For example, if you wanted the cron to send you
e-mail to warn you that your taxes are due April 15th,
and you want to be warned starting in January until
they are due in April, you could create a cron file
with the value 1-4 in the month field, and
the cron would run starting in January until
April. You can specify a list of values by separating
the numbers with a comma. For example, 1,7,9,10
would be the months January, July, September, and October.
Skip values can be specified with the / sign.
For example, 1-12/2 would be every other month.
Names can also be used for the month and day of the
week fields. The first three letters of the month or
day can be used. This option is not allowable with ranges
or lists.
Here are some additional
examples of valid time/date values:
|
Example: |
What
it does (examples are in the hour field) |
|
8-12 |
Event will execute
each hour in the range 8,9,10,11,12 |
|
1,4,5,7 |
Event will execute
each hour specified 1,4,5,7 |
|
0-4,8-12 |
Event will execute
each in the two ranges |
|
0-23/2 |
Event will execute
every other hour 2,4,6,8.... |
|
*/2 |
Same as above |
The sixth field in a
cron file (i.e., rest of the cron line)
are where you place the command you want to run. The
entire command portion, up to the newline character
or the % character will be executed by /bin/sh
(or the shell you have specified with the SHELL
environmental variable). Percent signs in the command,
unless they are escaped with a backslash (\)
will be changed into newline characters and all data
after the first % will be sent to the command
as standard input.
Example cron for mailing
a notice about taxes:
#
This is a comment.
SHELL=/bin/csh
MAILTO=johndoe@yourcompany.com
5 22 14 1-4 * mail -s "Your taxes are due
on April 15th"
judy@yourcompany.com%Judy,%%Fill out your taxes!%
Note: Do not place
hard returns in cron commands, because the line
wraps on its own. Hard returns tell cron that
the end of the cron command has occurred.
Example cron for deleting
logs monthly:
MAILTO=johndoe@yourcompany.com
1 3 * * * /usr/local/bin/virtual /usr/local/bin/vnukelog
-r
Notice the use of the
virtual command in the above example. The virtual
command is used to run scripts from the user's home
directory. It should be pointed out here that CRON
jobs do not run in the Virtual Server's environment.
They run in the physical server's environment, but they
run under the Virtual Server's User ID (a special number
that keeps track of users, what files they own, and
what processes they own). For this reason, when you
try and run scripts or programs from cron , you
must include the full path to the script. This includes
the path to your home directory. For example, if my
Telnet login were "judy", the path to my home
directory would be /usr/home/judy/. This is the
path from the physical server's root file structure.
Example cron for sending
a notice to occasionally mail information to judy:
01 09 14,30 1,3,5,7,8,10,12
* cat $HOME/etc/ cron file/my_ cron _file | /usr/bin/mail
-s "Message goes here" judy@yourcompany.com
Example cron for automating
stats with getstats:
40 19 * * * /usr/local/bin/getstats
-d -f | /usr/bin/mail -s "HTTP Daily stats"
judy@yourcompany.com
|