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

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 = ""

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")

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. Any VPS v2 user with shell access has the ability to create and use crontabs. Simply log in to the VPS v2 shell and 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

Please note: the information on this page applies to ITS web hosting plans. It may or may not apply to other environments. If you are looking for a feature described here, or better support from your hosting provider, please consider hosting your site with ITS!

1555 N Naperville/Wheaton Road, Suite 107
Naperville, IL 60563
phone 630.420.2550
fax 630.420.2771