Saturday, November 29, 2008

Crontab examples

Crontab is a text table, per user, used by crond to launch cron jobs.
A cron job is a task written in a line of a crontab, that runs on a specific time of day / week / month.
Other crontabs can be used always by the root/cron-default-user, like the ones at:
  • /etc/cron.d/
  • /etc/cron.daily/
  • /etc/cron.hourly/
  • /etc/cron.monthly/
  • /etc/cron.weekly/
On RedHat, for instance, a typical setting for a monthly run is:
  • /etc/cron.monthly/slocate.cron
In this case slocate.cron is a script file that has:
#!/bin/sh
renice +19 -p $$ >/dev/null 2>&1
/usr/bin/updatedb -f "nfs,smbfs,ncpfs,proc,devpts" \
-e "/tmp,/var/tmp,/usr/tmp,/afs,/net"
This will allow 'locate' database (typically at /var/lib/slocate/slocate.db) to be updated.
(Try locate my_filename_or_partial_filename, and you will understand what this is.)

Now back to cron jobs, per user. An example of a crontab:
[henrique@fuji poa_20081129]$ crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.2736 installed on Sat Nov 29 13:36:30 2008)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
MAILTO=""
EMAILTO=""

#30 23 * * * /home/henrique/yprog/scripting/linux_etc/cp_inx.sh
#*/5 * * * * /home/henrique/tiny/proxies.sh 1>/dev/null 2>/dev/null
#30 22 * * * /home/henrique/yprog/scripting/robot_g/robot_3pic3.sh &
#*/5 * * * * /usr/local/bin/gcntpaserver vmsvcd
55 */4 * * * /home/henrique/yprog/scripting/linux_etc/postoffice_freesco_analyst.sh
The first three lines are comments, to remember the file shown is temporary.
Then MAILTO="" or EMAILTO="" are suited to avoid mails if one of the cron job dumps output to stderr.
The first four crons are commented, thus are not ran.
The last cron says the script '...analyst.sh' is ran every four hours, at HH:55, i.e. runs at 0:55, 4:55, 8:55, 12:55, 16:55, and 20:55.