Tips & Tricks

Setting up cron jobs using crontab

This guide will show you how to setup a conjob using the crotab.

To install a cronjob, you will want to follow the following steps.

1. Enter username into following file:

user@UnixBox ~# vi /etc/cron.allow
Note: Be sure there is a blank line at the end of the file. You can always use a different text editor but this guide is showing it using vi.

2. Run crontab command:

– if you are logged in as root:
user@UnixBox ~# crontab -e -u username
Note: Here the username is the username owner of the cronjob.

– if you are logged in as the username that will be owner of the crontab:
user@UnixBox ~# crontab -e
Note: This can be executed as any user including the root if you are going to set a cronjob owned by the root.

The following is the syntax of the crontab:

[minute] [hour] [day of month] [month] [day of week] COMMAND

field allowed values
minute 0-59
hour 0-23
day of month 1-31
month 1-12
day of week 0-7 (0 or 7 is Sun)

You should prepend the COMMAND with nice. nice is a program that gives a given command a given priority. The default is 10. The range is from -20 to 20 with -20 being the highest priority.

Here is an example of running a nice’d command in the www/cgi-bin directory of a user every day at 11 PM (2300).

0 23 * * * nice ~/www/cgi-bin/command

~/ = current users home directory.. You should not have to, but you can also use ~username/ to further specify the given user. This is useful in case a process is launched by another user.

The “nice” option is used to limit the amount of resources that a cronjob can use. In some instances, you do not want to use the nice option. If the user is making calls to the perl library within their script, you would want to replace “nice” with “perl”. This is difficult to determine unless you can recognize within their script that they are making calls to the perl library. If in doubt, just leave the cronjob to run as nice, and if the customer reports that their cronjob is not running properly, then change it to “perl”.

For PHP scripts, you do not want to use nice. It is normally better to specify the path to the php binary before the path to the script. For example, replace “nice” with “/usr/bin/php”.

Leave a Reply