Title: General Help: Crontab - How do I set a cron job? Automate tasks by running programs at regular intervals using crontab.
What is Cron?
Cron is a daemon that allows you to automate tasks by running programs at regular intervals. You can define both the program that is to be run as well as the time of day, day of week, week of month, month of year, or any combination of these. A daemon is a server process that waits in the background until it is needed.
Cron runs on Unix/Linux machines, and currently is not available for Win32 platforms. However, alternatives are offered for Win32 users.
Commonly called "background running processes", cron is active from the time the computer is turned on to when the computer is turned off or cron is rebooted. Cron wakes up every minute, examines its list of things to do, and checks to see if it should be run in the current minute. If it has a program to run, it runs it. If it does not, it goes back to sleep for another 59 seconds.
This list of things to do is called a cron table, or crontabs for short. The crontabs is a schedule that is lists commands to perform, and the date/time to run them.
As you can now see, cron can make a webmaster's life much easier by automating functions that normally would be done manually. It's much better to focus your attention on obtaining new content and managing existing content if the "day to day" necessities are automated.
How do I know if I have cron access?
Because of the possible server burdens of enabling cron, many server administrators disable access to cron. Of course, the easiest way to learn if you have cron access is to ask your server administrator. If you'd rather not, and you have telnet access, simply type:
crontab -l
If you have cron access, you may see something similar to:
ares:/home3 $ crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (nothing installed on Mon Nov 29 23:06:56 1999)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
If you do not have crontab access, you will see an error message similar to:
You are not allowed to use this program (crontab)
See crontab(1) for more information
If you receive this error, you may wish to contact your server administrator and ask permission to use cron. Tell them the purposes why you need cron to be turned on and assure them that you will not use it for anything that involves server security or extremely high CPU loading.
How do I use cron?
We've found that one of the easiest ways to maintain multiple crontab entries is to create a simple text file with each program to be run on its own line and then to import the text file into your crontab. While there is another method of editing crontabs (by using the crontab -e command), we'll use the text file method. This way, you can also keep a backup copy of your crontab entries on your local hard drive and re-upload it if necessary.
Crontab entries are split into 2 fields, the date/time field and the command path. The date/time field is made up of 5 distinct components. Below is a table of these components and valid assignments in the order they are used in the crontab entry:
| Component | Acceptable Range |
|---|
| Minute of the Hour | 0-59 | | Hour of the Day | 0-23 | | Day of the Month | 1-31 | | Month of the Year | 1-12 | | Day of the Week | 0-6
(0=Sun, 6=Sat) |
This first field is delimited with spaces, so in our example, we wanted to set the program to run at 12:00am (00:00 in 24-hr format)each Monday. Here's what the crontab entry will may like:
0 0 * * * /home/upoint/www/cgi-bin/autodelete.cgi
Let's match this line with the scheduling commands they represent:
| 0 | 0 | * | * | 1 | /path/to/command |
|---|
| Minute | Hour | Day | Month | Weekday | Command |
|---|
The asterisks (*) mean that the program should run in all instances of the field. In the first example above, we want the crontab to be run each Monday, which means that it should be allowed to run on any day of the month (Monday can fall on the 1st in one month, on the 5th in another, etc) and any month of the year.
Similarly, to run a program once a day at exactly 12:01am (00:01 in 24-hr format), you use:
1 0 * * * /home/upoint/www/invoiceme/reminder/autoreminder.cgi
The program path you see is the full server path to the program that's to be run. It's extremely important to note that if the program to be run requires any libraries does, you should always use the full server path to the libraries within the program that's to be run. This is because cron runs from the root directory of your account. Since it's essentially including the program that's to be run, it's running the program from the root directory, and not from the directory your program resides in. So if you're using any relative paths to libraries, your program will not be able to find them in the root directory and the program will fail.
What's extremely useful is that all output from the program, if any, will be sent to the email address defined in your $ENV{' SERVER_ADMIN'} environment variable (if defined). Therefore, you can receive confirmation of successful runs or failures. Remember, all output from a Perl program (such as with the print command) will automatically be directed to STDOUT, whose contents will automatically be sent via email (if defined). You can also add a line which will define (or temporarily change) the $ENV{'SERVER_ADMIN'} variable. In your text file, add:
MAILTO=you@yourdomain.com
Let's get back on track. After you've completed your text file, save it to .... say the root directory of your server. In our example, let's call it cronentries.txt. Now telnet to your server, change directory to the root. Example:
cd /home/upoint/www
then type:
crontab cronentries.txt
If you get no error and the command prompt reappears, the commands and timetable you've entered in your text file have been imported into the crontab. To double-check, type:
crontab -l
Here, you'll see the line(s) you entered in your cronentries.txt.
How do I delete a crontab?
Easy. Simply telnet to your account, change directory to the root (in this example) and type:
crontab -r
That's all there is to it.
~ Original tutorial courtesy of PerlArchive.com
Powered by Document Publisher |