Friday, September 19, 2008

scheduling a task to automatically run using cron

To get cron to automatically run something, it must be listed in a crontab file. Each user has his own crontab file. To edit a user's crontab file, use the command crontab -e (a crontab file will be created if none exists).

An entry in a crontab file looks like this:

m h dom mon dow command

where m = minutes, h = hours, dom = day of month, mon = month, and dow = day of week. cron will automatically run the specified command whenever the time matches the specified time. To indicate "any", use an asterisk (*). So to get something to run at 5pm every day, you would add the following line to your crontab file:

00 17 * * * cmd-name

Make sure to use crontab -e to edit your crontab file. The actual files are stored in /var/spool/cron/crontabs/, but you shouldn't edit those files directly.

Pasted from wikipedia:

# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
 *  *  *  *  *  command to be executed

Update 2012-08-13: still can't get cron working (Lucid)

This page provided some hints: http://aplawrence.com/Unixart/crontab_not_running.html

/var/log/syslog did not show the cron jobs running

chkconfig --list | grep cron indicated that cron was off:

cron                      0:off  1:off  2:off  3:off  4:off  5:off  6:off

No comments: