Cron Jobs - Processing Scripts Automatically
Articles et actualités de la base de connaissances
Cron Jobs - Processing Scripts Automatically
Cron Jobs (Automated Job Processing)
- The use of "cron jobs", which are processes or scripts that are run automatically at certain times, in accordance with a "crontab" file set up by each user, are allowed on our servers, subject to the following conditions and restrictions:
- The job must not execute more often than every two hours.
- The job must generate no greater impact than a normal CGI script. In other words it must not disrupt the normal operations of our servers or slow down our server processing in any way.
- The job must be "niced". For details, read about the nice command. The preferred value is 10 or greater.
To use cron, you must first set up a file of cron entries. This file can actually be placed anywhere in your account you like, and have any name you want. It should contain one entry for each action you want taken.
A cron entry looks similar to the following:
30 3 * * * /usr/local/bin/nice -20 /usr/home/username/script.pl
In this case, the script "script.pl" would be run nightly at 3:30 AM.
"/usr/bin/nice -20" utilizes the system nice command to lower the impact of the call on the server. We recommend that you use it with all your cron jobs, to best preserve system resources.
"/usr/home/username/script.pl" is the full path to the script/program you would like cron to run.
You can also use regular UNIX commands here if you like, using full paths to programs.
For instance, you could have
30 3 * * * /usr/local/bin/nice -20 /bin/rm /usr/home/username/temp/*to remove all files from some temporary directory you might have each night at 3:30 AM, as in the earlier example. In this case the command remove (rm) is used. You can find where any command resides by typing this in telnet whereis command. This will give you the proper directory on your server.
The "30 3 * * *" requires a bit more explaining. The time a cron command is executed is controlled by the 5 numbers that precede the cron directive.
The first number is the minutes after the hour (0-59).
The second is the hour of the day (0-23, with 0 being 12 AM).
The third number is the day of the month (1-31).
The fourth is the month of the year (1-12).
The fifth is the day of the week (0-6, with 0=Sunday and 6=Saturday, etc).
A * in place of any of the values matches all possibilities (for instance, in the given example the script runs every day because the *'s for day of the month, day of the week, and month of the year match all values).
Multiple values are separated by commas. For instance, you might start with 15 1,3,5 * * * to run a script every day at 1:15, 3:15, and 5:15. (Note that our policies prohibit a program running via cron more often than once every two hours.)
Once you have your cron directive file (say, cron.file) in your account, you must connect via telnet and issue the command:
crontab cron.file
using the name you gave the file. (Note that you must be in the same directory as the cron file at the time.) After this, your cron should be activated. If the programs running via cron generate errors, the cron daemon will e-mail them to your account.
If at any time you would like to change your cron jobs, just edit your file, and rerun the crontab command as shown above.