| |
Cron - Program Scheduler
Your Virtual Private Server includes access to the cron daemon which is used to
automatically execute scheduled commands. The list of commands you schedule cron to run
is stored in a cron table, or crontab. Using your own crontab, you can schedule your
Virtual Private Server to run a certain command at a predetermined date and time.
Creating a Crontab
Your Virtual Private Servers crontab can be stored in any file you choose. For example, you
may wish to create or upload a file called cronjobs in your Virtual Private Server home
directory for your crontab. Each line in your crontab will either be an environment variable
setting, a cron table entry, or a comment (beginning with the # symbol).
An environment variable setting in a crontab looks like this:
NAME = VALUE
Several environment variables are set up automatically by the cron daemon, depending on your
Virtual Private Server O/S.
FreeBSD & BSD/OS
Upgrade!
The cron environment variables include SHELL, LOGNAME, USER, and HOME. In addition to these,
there is a special MAILTO environment variable. Any output generated by your cron jobs will be
sent to the address specified by MAILTO (if it is not specified it will be sent to the owner
of the crontab). If MAILTO is defined as an empty string then no mail will be sent.
MAILTO = ""
Solaris
The cron environment variables include SHELL, LOGNAME, USER, and HOME. In order to use
command and file paths that include the tilde (~ character, set the SHELL variable
like this:
SHELL = "/bin/bash"
NOTE: Solaris does NOT use the MAILTO variable. You can
use the mail command to send E-mail output of cron jobs. For example, to mail the
output of a script to a user, you can format the cron command similarly to the following
(keeping everything on a single line in your crontab).
* * * * * /usr/home/username/scriptname.sh > /bin/mail myuser@mydomain.com
|
The format of a cron table entry includes five (5) time fields followed by a command. Commands
are executed when the time specified by the date fields matches the current time. The five time
fields are as follows:
Field Allowed Values
----- --------------
Minute 0-59
Hour 0-23
Day of Month 1-31
Month 1-12, jan, feb, mar, apr, may, jun, jul, aug, sep, oct,
nov, dec
Day of Week 0-7, sun, mon, tue, wed, thu, fri, sat (0 and 7 are "sun")
NOTE: In the Day of Week field, you can only use 7 as
a value in FreeBSD or BSD/OS. Solaris does not recognize the 7 as a valid day. |
A field may be an asterisk (*), which indicates all values in the range are acceptable.
Ranges of numbers are allowed, i.e. 2-5 or 8-11, and lists of numbers are
allowed, i.e. 1,3,5 or 1,3,8-11. Step values can be represented as a sequence,
i.e. 0-59/15, 1-31/3, or */2.
The actual command you wish to execute, including any parameters to be passed to it, is
the sixth, and final field of a cron table entry.
NOTE: Each crontab entry must have a trailing line break
in order for the cron table entry to be recognized. Because of this, you may want to hit
Enter a few times at the end of your crontab file when you are editing it. |
Some examples of complete cron table entries are show below, implementing the
vnukelog command as an example.
# Any output generated by the cron entries below is sent to the e-mail
# address assigned to the MAILTO environment variable.
MAILTO="webmaster@mycompany.com"
# Execute the "vnukelog" command at 1:15 (15 1) AM every day.
15 1 * * * /usr/local/bin/vnukelog
# Execute the "vnukelog" command at 11:40 PM (40 23) on the first day (1)
# of each month.
40 23 1 * * /usr/local/bin/vnukelog
# Execute the "vnukelog" command every 10 minutes for for the first
# half-hour (0-30/10) of the 9:00 AM and 5:00 PM hours (9,17) on
# Monday-Friday (1-5).
0-30/10 9,17 * * 1-5 /usr/local/bin/vnukelog
# Execute the "vnukelog" command at 4:00 AM, 8:00 AM, 12:00 noon, 4:00 PM,
# and 8:00 PM (0 */4) on each Sunday (sun) every January (jan).
0 */4 * jan sun /usr/local/bin/vnukelog
# Execute the "vnukelog" command at 4:30 AM (30 4) on the first, fifteenth
# (1,15), and each Friday (fri) of every month.
30 4 1,15 * fri /usr/local/bin/vnukelog
# Execute the "vnukelog" command at 12:00 midnight (0 0) on August 19 (8)
# (aug).
0 0 19 8 * /usr/local/bin/vnukelog
0 0 19 aug * /usr/local/bin/vnukelog
Installing a Crontab
After you have defined the cron table entries in your cronjobs file, you will need to register
your crontab with the system. This can be done by running the crontab command. For
example, if you created your crontab file with the name cronjobs in your Virtual Private
Server home directory then you would use the following command:
% crontab ~/cronjobs
This will register your crontab file with the cron system daemon. If you ever need to review
the current cron entries you have registered with the cron system daemon, you need simply use
this command:
% crontab -l
Documentation
Issue the following commands to view the cron and crontab man pages.
% man cron
% man crontab
% man 5 crontab
|