10.5 Running Programs from Cron

You may wish to run scripts or other administration tools in an automated fashion on a regular basis. The Unix cron program will allow you to do this. If you run crontab -e from the command line, it will bring up an editor. [5] In this editor, you can add or edit the names of programs to be run. The account from which you run the crontab program is the account that will execute the programs later on. If the program should be run as root, make sure to run crontab as root when you add the entry.

[5] If you are not happy with the default editor, which may be ed , you can override it by setting the EDITOR environment variable to the editor of your choice.

Each line of the file specifies one program to run. The first five fields denote the time the program should be started. They are, in order:

  • The minute (0 “59)

  • The hour (0 “23)

  • The day of the month (1 “31)

  • The month (1 “12)

  • The day of the week (0 “6, 0 is Sunday)

An asterisk can be used in any of these fields to indicate that every value is acceptable. For example, 00 1 * * * would mean that the program should be run every day at 1:00 a.m.

Following the timing information is the name of the program to run and its arguments. For example:

 
 Solaris# crontab -l 37 * * * * /usr/lib/sendmail -q > /dev/null 2> /dev/null 

As you can see, running the crontab program with the -l option will print the current configuration. Here the mail queue is run every hour at 37 minutes past the hour. The standard output and standard error are redirected to /dev/null because, otherwise , cron will send output in email, and in this case, it was decided that it would be preferable not to receive any error messages.

Here's another example:

 
 Solaris# crontab -l 05 9 * * * /usr/local/bin/report.pl -e > /dev/null 

This runs a report program every day at 9:05 a.m. The program sends email to administrators about problem conditions that have arisen within the past day.

After you edit the crontab file, save and exit the editor, and the changes will be complete. You should test the script to make sure it works correctly from cron .



Open Source Network Administration
Linux Kernel in a Nutshell (In a Nutshell (OReilly))
ISBN: 130462101
EAN: 2147483647
Year: 2002
Pages: 85

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net