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 and other 'NIX machines, and currently is not available for Win32 platforms. However, alternatives are offered for Win32 users; one such is available at Freeware. 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 the 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 executes 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 lists commands to perform, and the date/time to run them.

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 had rather not, (some admins have an ATTITUDE!) 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, admin or SysOp and ask permission to use CRON. If they give you the usual runaround and tell you NO!, you may want to use a CRON emulator such as our MUL8CRON script to effectively do the same thing without CRON. Contact us about it.

How do I use CRON? Okay… let's assume you have CRON access and wish to run a program that backs up your access log weekly. You have learned from your server administrator that your access log is cleared out weekly on Mondays at 12:00am. You have a site statistics program that requires CRON so it can read the access log on a regular schedule and generate your statistics for you. You've also downloaded and installed a file backup program (such as BackMeUp) and now want to set up a CRONtab entry to run the program every week. We have 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 will use the text file method. This way, you can also keep a backup copy of your CRONtab entries on your local hard drive and upload again at a later time 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

In the table above, the 0 in day of the week is Sunday; 6 is Saturday. This first field is delimited with spaces, so in our example above, we wanted to set the program to run at 12:00am each Monday. Here's what the CRONtab entry will look like:

0 0 * * * 1 /home/perlarchive/www/cgi-bin/bmu.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. A '0' in the day or month field means every. In the above example, we want the CRONtab to be run each Sunday, which means that it should be allowed to run on any day of the month (Sundays can fall on the 1st in one month, on the 5th in another, etc) and any month of the year. The program path you see is the full server path to the program that's to be run. It is extremely important to note that if the program to be run requires any libraries (as BackMeUp) does, you should always use the full server path to the libraries within the program that needs to be run. This is because CRON runs from the root directory of your account. Since it is essentially including the program that is to be run, it must run the program from the root directory, and not from the directory (your) that the program resides in. So if you are 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 is extremely useful is that all output from the program, if any, will be sent to the E-Mail address defined in your $ENV{' SERVER_ADMIN'} environment variable (if it is defined). Therefore, you can receive confirmation of successful runs or failures. Remember that 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 E-Mail (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@anysite.com

After you have created and completed your text file, save it. (If you still need help, you might consider using our CRON Job Calculator.) In our example, let's call it cronentries.txt. Now type (assuming you have command line capability):

crontab cronentries.txt

Done! The commands and timetable you have created and entered in your text file have been imported into the CRONtab. To check your efforts, type:

crontab -l

Here, you'll see the line(s) you entered in your cronentries.txt. How do I delete a CRONtab? No problem! Simply telnet to your account and type:

crontab -r

That's all there is to it. Where can I find out more about CRON? While you have enough information now to set a CRONtab, there are other advanced functions and features of CRON that have not been discussed. Some system versions have very advanced features and others do not. You can continue your education if necessary at the following sources:

Freeware

UNIX Help For Users

UNIX and Linux Commands

UNIX Beginners Start Here

Co-operating Sponsors and Technology used on our Website


International Copyright Violation
Registered® Trademark™ and Copyright© 1973 - CSG, Computer Support Group, Inc. and CSGNetwork.Com All Rights Reserved