search by tags

for the user

adventures into the land of the command line

scheduling tasks with cron

cron is a time-based job scheduler. you can use cron to schedule jobs, commands or shell scripts to run periodically at fixed times, dates, or intervals.

there are two popular versions: anacron & vixiecron

crond is the daemon

$ /etc/init.d/crond

cron’s configuration files are

$ /etc/sysconfig/crond
$ /etc/cron.allow
$ /etc/cron.deny

allow and deny are a whitelist and a blacklist for users

system crontabs which are the files in which you schedule the jobs you want to run are in a few places

$ /etc/crontab # default
$ /etc/cron.d/* # application specific
$ /etc/cron.{daily,hourly,monthly,weekly} # interval specific
$ run-parts/crontabs (crontabs(4)) # run all executable scripts in directory (won't descend into subdirectories)

user crontabs are put in this directory

$ /var/spool/cron/

checking your user’s crontab

$ crontab -l

checking another user’s crontab

$ crontab -lu user

edit your user’s crontab

$ crontab -e

edit another user’s crontab

$ crontab -eu user

remove your user’s crontab

$ crontab -r

crontab syntax

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

this command will do something every 15 minutes

0,15,30,45 * * * * /opt/puppet/sbin/refresh-mcollective-metadata 2>&1 >>/var/log/pe-mcollective/mcollective-metadata-cron.log

things to look out for:

email flood from /var/spool/mail/user
daylight savings
PATH. path must be declared in the system crontab, or commands used in a crontab must have the full path to the command

eg. this will work

*/1 * * * * /bin/sh /home/user/rsync.sh >> /home/user/rsync.log 2>&1

this won’t

*/1 * * * * sh /home/user/rsync.sh >> /home/user/rsync.log 2>&1

changes to spool wont be picked up by directly editting the user crontab files inside. crontab -e will be picked up straight away and should be used to make changes to a crontab