5.12. Using Cron

Each user can use cron to run jobs on a periodic basis. To create your own crontab:

% crontab -e

Entries in the user's crontab file are separated by a TAB. Full pathnames should be used. A simple example of an hourly task:

#mn     hr      dy      mo      wd      command
0       0-23    *       *       *       /home/rpratt/bin/do_it.sh
           field         allowed values
           -----         --------------
           minute        0-59
           hour          0-23
           day of month  1-31
           month         1-12 (or names, see below)
           day of week   0-7 (0 or 7 is Sun, or use names)

Cron will mail any output produced on stdout or stderr by the script. If you don't want this e-mail then you have three choices:

Set the MAILTO= variable at the top of the crontab (See the crontab(5) man page). This won't suppress the e-mail, just redirect it somewhere else. Instead of choosing your least favourite user as a sacrificial victim, sending the mail to 'nobody' will dispose of it humanely. Setting MAILTO="" will suppress all e-mail output from all cron jobs.

Redirection of stdout and stderr of your command to a log file, or to /dev/null. Append >/dev/null 2>&1 to the end of the command line in the crontab to send all the output to oblivion:

#mn     hr      dy      mo      wd      command
0       0-23    *       *       *       /home/rpratt/bin/do_it.sh > /dev/null 2>&1

The output can also be redirected to some logfile:

#mn     hr      dy      mo      wd      command
0       0-23    *       *       *       /home/rpratt/bin/do_it.sh >> /var/tmp/logfile 2>&1

The best way is to rewrite the script so that there is no output for a successfully completed run. Its reasonable to include a means to detect problems and send an email should something go wrong with the execution of the script.