Cron
Cron is a daemon that executes scheduled
commands. Cron is started automatically from /etc/init.d on entering multi-user
runlevels. Cron searches its spool area (/var/spool/cron/crontabs) for crontab
files (which are named after accounts in /etc/passwd); crontabs found are
loaded into memory. Note that crontabs in this directory should not be accessed
directly - the crontab command should be used to access and update them.
Cron also reads /etc/crontab, which is in a slightly
different format. Additionally, cron reads the files in /etc/cron.d.
Cron then wakes up every minute, examining all stored
crontabs, checking each command to see if it should be run in the current
minute. When executing commands, any output is mailed to the owner of the
crontab (or to the user named in the MAILTO environment variable in the
crontab, if such exists). The children copies of cron running these processes
have their name coerced to uppercase, as will be seen in the syslog and ps
output.
Additionally, cron checks each minute to see if its spool
directory's modtime (or the modtime on /etc/crontab) has changed, and if it
has, cron will then examine the modtime on all crontabs and reload those which
have changed. Thus cron need not be restarted whenever a crontab file is
modified. Note that the crontab(1) command updates the modtime of the spool
directory whenever it changes a crontab.
Special considerations exist when the clock is changed by
less than 3 hours, for example at the beginning and end of daylight savings
time. If the time has moved forwards, those jobs which would have run in the
time that was skipped will be run soon after the change. Conversely, if the
time has moved backwards by less than 3 hours, those jobs that fall into the
repeated time will not be re-run.
Only jobs that run at a particular time (not specified as
@hourly, nor with '*' in the hour or minute specifier) are affected. Jobs which
are specified with wild cards are run based on the new time immediately.
Clock changes of more than 3 hours are considered to be
corrections to the clock, and the new time is used immediately.
In Debian and Redhat cron treats the files in /etc/cron.d as
extensions to the /etc/crontab file (they follow the special format of that
file, i.e. they include the user field). The intended purpose of this feature
is to allow packages that require finer control of their scheduling than the
/etc/cron.{daily,weekly,monthly} directories allow to add a crontab file to
/etc/cron.d. Such files should be named after the package that supplies them.
Files must conform to the same naming convention as used by run-parts: they
must consist solely of upper- and lower-case letters, digits, underscores, and
hyphens. Like /etc/crontab, the files in the /etc/cron.d directory are
monitored for changes.
You should use absolute path names for commands like
/bin/ls. This is to insure you call the correct command.
Crontab
Crontab is the program used to install, deinstall or
list the tables used to drive the cron daemon in Vixie Cron. Each user can have
their own crontab, and though these are files in /var/spool/cron/crontabs, they
are not intended to be edited directly.
Each user has their own crontab, and commands in any given
crontab will be executed as the user who owns the crontab. Uucp and News will
usually have their own crontabs, eliminating the need for explicitly running su
as part of a cron command.
Blank lines and leading spaces and tabs are ignored. Lines
whose first non-space character is a hash-sign (#) are comments, and are
ignored. Note that comments are not allowed on the same line as cron commands,
since they will be taken to be part of the command. Similarly, comments are not
allowed on the same line as environment variable settings.
An active line in a crontab will be either an environment
setting or a cron command. An environ‐ ment setting is of the form: name =
value where the spaces around the equal-sign (=) are optional, and any
subsequent non-leading spaces in value will be part of the value assigned to
name. The value string may be placed in quotes (single or double, but matching)
to preserve leading or trailing blanks. The value string is not parsed for environmental
substitutions, thus lines like: PATH = $HOME/bin:$PATH will not work as you
might expect.
Several environment variables are set up automatically by
the cron daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the
/etc/passwd line of the crontab's owner. PATH is set to
"/usr/bin:/bin". HOME, SHELL, and PATH may be overridden by settings
in the crontab; LOGNAME is the user that the job is running from, and may not
be changed. Another note: the LOGNAME variable is sometimes called USER on BSD
systems... on these systems, USER will be set also.
In addition to LOGNAME, HOME, and SHELL, cron will look at
MAILTO if it has any reason to send mail as a result of running commands in
"this" crontab. If MAILTO is defined (and non-empty), mail is sent to
the user so named. If MAILTO is defined but empty (MAILTO=""), no
mail will be sent. Otherwise mail is sent to the owner of the crontab.
If the /etc/cron.allow file exists, then you must be listed
therein in order to be allowed to use this command. If the /etc/cron.allow file
does not exist but the /etc/cron.deny file does exist, then you must not be
listed in the /etc/cron.deny file in order to use this command. If neither of
these files exists, then depending on site-dependent configuration parameters,
only the super user will be allowed to use this command, or all users will be
able to use this command. For standard Debian systems, all users may use this
command.
If the -u option is given, it specifies the name of the
user whose crontab is to be tweaked. If this option is not given, crontab
examines "your" crontab, i.e., the crontab of the person executing
the command. Note that su can confuse crontab and that if you are running
inside of su you should always use the -u option for safety's sake.
The first form of this command is used to install a new
crontab from some named file or standard input if the pseudo-filename ``-'' is
given.
The -l option causes the current crontab to be displayed on
standard output.
The -r option causes the current crontab to be removed.
The -e option is used to edit the current crontab using the
editor specified by the VISUAL or EDITOR environment variables. The specified
editor must edit the file in place; any editor that unlinks the file and
recreates it cannot be used. After you exit from the editor, the modified
crontab will be installed automatically.
On the Debian GNU/Linux system, cron supports the pam_env
module, and loads the environment specified by /etc/security/pam_env.conf.
However, the PAM setting do NOT override the settings described above nor any
settings in the crontab file itself. Note in particular that if you want a PATH
other than "/usr/bin:/bin", you will need to set it in the crontab
file.
By default, cron will send mail using the mail
"Content-Type:" header of "text/plain" with the
"charset=" parameter set to the charmap / codeset of the locale in
which crond is started up - ie. either the default system locale, if no LC_*
environment variables are set, or the locale specified by the LC_* environment
variables ( see locale(7)). You can use different character encodings for
mailed cron job output by setting the CONTENT_TYPE and
CONTENT_TRANSFER_ENCODING variables in crontabs, to the correct values of the
mail headers of those names.
Crontab Format
Commands are executed by cron when the minute, hour, and
month of year fields match the current time, and when at least one of the two
day fields (day of month, or day of week) match the current time.
A field may be an asterisk (*), which always stands for
"first-last".
Ranges of numbers are allowed. Ranges are two numbers
separated with a hyphen. The specified range is inclusive. For example, 8-11
for an "hours" entry specifies execution at hours 8, 9, 10 and 11.
Lists are allowed. A list is a set of numbers (or ranges)
separated by commas. Examples: "1,2,5,9", "0-4,8-12".
Step values can be used in conjunction with ranges.
Following a range with "/" specifies skips of the number's value
through the range. For example, "0-23/2" can be used in the hours
field to specify command execution every other hour (the alternative in the V7
standard is "0,2,4,6,8,10,12,14,16,18,20,22"). Steps are also
permitted after an asterisk, so if you want to say "every two hours",
just use "*/2".
Names can also be used for the "month" and
"day of week" fields. Use the first three letters of the particular
day or month (case doesn't matter). Ranges or lists of names are not allowed.
The "sixth" field (the rest of the line)
specifies the command to be run. The entire command portion of the line, up to
a newline or % character, will be executed by /bin/sh or by the shell specified
in the SHELL variable of the crontab file. Percent-signs (%) in the command,
unless escaped with backslash (\), will be changed into newline characters, and
all data after the first % will be sent to the command as standard input. There
is no way to split a single command line onto multiple lines, like the shell's
trailing "\".
Note: The day of a command's execution can be specified by
two fields - day of month, and day of week. If both fields are restricted
(i.e., aren't *), the command will be run when either field matches the current
time. For example, "30 4 1,15 * 5" would cause a command to be run at
4:30 am on the 1st and 15th of each month, plus every Friday.
Instead of the first five fields, one of eight special
strings may appear:
string
meaning
------
-------
@reboot Run
once, at startup.
@yearly Run
once a year, "0 0 1 1 *".
@annually (same
as @yearly)
@monthly Run
once a month, "0 0 1 * *".
@weekly Run
once a week, "0 0 * * 0".
@daily Run
once a day, "0 0 * * *".
@midnight (same
as @daily)
@hourly Run
once an hour, "0 * * * *".
An example of crontab format with commented fields is as
follows:
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31)
(1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * 0,6 /usr/bin/find
This line executes the "find" command at 2AM on
the 12th of every month that a Sunday or Saturday falls on.
Examples
Here are some more examples of crontab lines. Use the
command "crontab -e" to edit your crontab file.
This line executes the "ping" command every
minute of every hour of every day of every month. The standard output is
redirected to dev null so we will get no e-mail but will allow the standard
error to be sent as a e-mail. If you want no e-mail ever change the command
line to "/sbin/ping -c 1 192.168.0.1 > /dev/null 2>&1".
* * *
* * /sbin/ping -c 1 192.168.0.1 >
/dev/null
This line executes the "ping" and the "ls"
command every 12am and 12pm on the 1st day of every 2nd month. It also puts the
output of the commands into the log file /var/log/cronrun.
0 0,12 1 */2 * /sbin/ping -c 192.168.0.1; ls -la
>>/var/log/cronrun
This line executes the disk usage command to get the
directory sizes every 2am on the 1st through the 10th of each month. E-mail is
sent to the email addresses specified with the MAILTO line. The PATH is also
set to something different.
PATH=/usr/local/sbin:/usr/local/bin:/home/user1/bin
MAILTO=user1@nowhere.org,user2@somewhere.org
0 2 1-10 * * du -h --max-depth=1 /
This line is and example of running a cron job every month,
on Mondays whose dates are between 15-21. This means the third Monday only of
the month at 4 a.m.
0 4 15-21 * 1 /command
Things to look out for! Gotchas!
When cron job is run from the users crontab it is executed
as that user. It does not however source any files in the users home directory
like their .cshrc or .bashrc or any other file. If you need cron to source
(read) any file that your script will need you should do it from the script
cron is calling. Setting paths, sourcing files, setting environment variables,
etc.
If the users account has a crontab but no useable shell in
/etc/passwd then the cronjob will not run. You will have to give the account a
shell for the crontab to run.
If your cronjobs are not running check if the cron deamon
is running. Then remember to check /etc/cron.allow and /etc/cron.deny files. If
they exist then the user you want to be able to run jobs must be in
/etc/cron.allow. You also might want to check if the /etc/security/access.conf
file exists. You might need to add your user in there.
Crontab is not parsed for environmental substitutions. You
can not use things like $PATH, $HOME, or ~/sbin. You can set things like
MAILTO= or PATH= and other environment variables the /bin/sh shell uses.
Cron does not deal with seconds so you can't have cronjob's
going off in any time period dealing with seconds. Like a cronjob going off
every 30 seconds.
You can not use % in the command area. They will need to be
escaped and if used with a command like the date command you can put it in
backticks. Ex. `date +\%Y-\%m-\%d`
Helpful
Cron Sites
No comments:
Post a Comment