Environment variable settings
Each environment variable
line consists of a variable name, an equal sign
(=), and a value. Values that contain
spaces need to be enclosed within quotes.
The following are some examples of environment
variable settings:
color = red
ORACLE_HOME =
/u01/app/oracle/product/10.2.0/db_1
title
= ‘My Life in a Nutshell’
As with the operating system
itself, variable names in the crontab are
case-sensitive and system variables are usually
defined with upper case names, while user
defined variables are defined with lower case
names.
Crontab command Lines
Each crontab command line is
comprised of six positional fields specifying
the time, date and shell script or command to be
run. The format of the crontab command
line is described in Table 4.2:
|
|
Minute
|
0-59
|
Hour
|
0-23
|
Day of Month
|
1-31
|
Month
|
1-12
|
Day of Week
|
0-7
|
Command
|
Command path/command
|
Table 4.2:
Crontab
Command Line Format
In all fields which take
numeric values, a single number, a range of
numbers indicated with a hyphen (such as 2-4), a
list of specific values separated by commas
(like 2,3,4) or a combination of these
designations separated by commas (such as 1,3-5)
are allowed. Also allowed is an asterisk
(*) indicating every possible value of this
field. All conditions must be met for the
entry to execute.
This can all get rather confusing so make
sure that one understands exactly what an entry
is doing before making any changes to it.
This
entry will run the script
cleanup.ksh
at 0 minutes past the hour, 6 am, every Monday. This
illustrates that for a crontab to execute, all
of the conditions specified must be met. So even
though every day of the month has been said by
making the third field a wildcard, the day also
has to meet the final condition that the day is
a Monday.
#**********************************************************
# Run the Weekly Management Report every
Monday at 7:00 AM
# and save a copy of the report in
my /home directory
#**********************************************************
00 07 * * 1 /home/bei/scripts/weekly_mgmt_rpt.ksh
wprd > /home/terry/weekly_mgmt_rpt.lst
This
entry is similar to the previous entry, but will
execute at
7:00am. Since the hour is in 24
hour format (midnight is actually represented as
00), the 07 represents 7:00 a.m. This
entry again will only be run on Mondays.
#**********************************************************
# Lunch Time Notification - run
Monday-Friday at Noon -
# sends a message to all users indicating it's
lunch time
#**********************************************************
00 12 * * 1-5 /home/terry/lunch_time.ksh
wprd > /tmp/lunch_time.lst
This lunch reminder is set up
to run at
12:00 p.m. Monday through Friday
only.
The most important thing to
remember is that a crontab entry will execute
every time all of its conditions are met.
To take the last entry as an example, anytime it
is 00 minutes past the hour of 12 on any day of
the month and any month of the year and the day
of the week is between Monday and Friday
inclusive (1-5), this crontab will be executed.
Most crontab entries will
have some wildcards, but be careful where they
are used. For instance, if a * was
mistakenly placed in the minute position of the
last crontab example above, the script would end
up running for ever minute of the 12:00 hour
instead of just once at the beginning of the
hour.
Remember that the wildcard means all, not
'any'.
The day-of-week field accepts
either zero or seven as a value for Sunday.
Any of the time/date fields can also contain an
asterisk (*) indicating the entire range of
values. Additionally, month and
day-of-week fields can contain name values,
consisting of the first three letters of the
month, as indicated in Table 4.3.
Field
|
Valid Entries (case
insensitive)
|
Days of the week
|
sun, mon, tue, wed, thu, fri, sat
|
|
SUN,
MON, TUE, WED, THU, FRI, SAT
|
Months of year
|
jan, feb, mar, apr,
may, jun, jul, aug, sep, oct, nov, dec
|
|
JAN, FEB,
MAR, APR,
MAY, JUN, JUL, AUG,
SEP, OCT, NOV, DEC
|
Table 4.3:
Month and Day-of-week Fields
When
numbers are used, the user can specify a range
of values separated by a hyphen or a list of
values separated by commas. In other
words, specifying 2-5 in the hour field means 2AM, 3AM, 4AM
and 5AM,
while specifying 2,5 means only
2AM and 5AM.
Most
people will execute shell scripts from the
crontab, but any operating system command can be
executed. If the command or script called in the
crontab typically sends output to the screen,
the move may be to redirect that output to a log
file with the
>> symbol so it can be
checked later.
The > redirection symbol will create a
new file, while the >> symbol will append to an
existing file, or create a new one if it does
not exist.
Generally, a new
log file will be created
with each execution, and the log file will be
periodically reviewed for problems.
By
default, the output from a job is emailed to the
owner of the job or the user specified by the
mailto
variable. If this is unacceptable, the
output can be redirected in a number of ways,
including:
# Mail the output of the
job to another user.
command | mail -s
"Subject: Output of job" user
# Standard output
redirected to a file.
command >> file.log
# Standard output and
standard error redirected to a file.
command >> logfile 2>&1
# Throw all the output
away