CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in
Add ListingModify ListingTell A FriendLink to TPASubscribeNew ListingsCool ListingsTop RatedRandom Link
Newest Reviews
  • review
  • hagen software
  • NOT GPL!
  • Hagan Software
  • Wasted Time with ...
  • poor pre-sale sup...
  • no response
  • rating the offer
  • Good Stuff
  • Good idea but use...


  •  
    Perl Archive : TLC : Programming : Perl : What is cron?
    Guide Search entire directory 
     

    Date Published: 2002-12-29

    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.

    The Perl Archive uses cron quite a bit each night. Here's some of the things we use cron nightly for:

    1. Reading the database of perl programs and rebuilding the Guide's HTML pages
    2. Checking links with the "Hunter"
    3. Performing backups of critical data.

    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.

    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've 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'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

    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 may 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. 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's 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'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. In our example, let's call it cronentries.txt. Now type:

    crontab cronentries.txt

    That's it - 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 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. You can continue your education if necessary at the following sources:

  • Unix Commands - OFS Man Pages - cron Command (CNS Electronic HelpDesk, University of Alberta)

    Written by D. Jasmine Merced

  •  
     


    About The Perl ArchiveLink Validation ProcessSearch Tips
    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives