Making sense of cron in Centos and Fedora Linux

CentOS and Fedora Linux use a set of directories in /etc to control when jobs run. These directories take the following form:

/etc/cron.hourly – jobs that run once per hour
/etc/cron.daily – jobs that run once per day
/etc/cron.weekly – jobs that run one per week
/etc/cron.monthly – jobs that run once per month

To add a job to run hourly, daily, weekly or monthly, you can drop an executable shell script in the pertinent directory. I recently became curious which time of the day daily jobs execute, which day weekly jobs run, and when monthly jobs were scheduled. The answer to these questions comes in the way of /etc/crontab, which contains crontab formatted entries that call the run-parts script to invoke the scripts in the hourly, daily, weekly or monthly directory:

$ more /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

If you are familiar with the crontab format, it’s easy enough to decode this information to figure out the days and times when cron jobs will run. Nice!

7 Comments

natxo asenjo  on August 22nd, 2009

Just don’t forget to *not* give an extension to the executable you drop there. Or it will not run (this bit me once :-) )

Paul W. Frields  on August 23rd, 2009

Extensions are perfectly permissible. You do need to make sure the executable bit is set on the file though.

Frank  on August 25th, 2009

Hm… odd. I was expecting to get the content of /etc/crontab with “crontab -l” as root…

justintime  on August 25th, 2009

Another key point is that the /etc/cron.daily, weekly, and monthly directories are run by anacron, whereas /etc/crontab and /etc/cron.d are run by vixie-cron. If your machine is always up, then there isn’t any difference, but on a system such as a laptop, this makes a big difference. Anacron will “catch up” on missed jobs if the system was powered off when jobs were supposed to run, where as the standard vixie cron will not.

Frank  on August 26th, 2009

justintime wrote on August 25th, 2009:

>Another key point is that the /etc/cron.daily, >weekly, and monthly directories are run by >anacron, whereas /etc/crontab and /etc/cron.d >are run by vixie-cron. If your machine is always >up, then there isn’t any difference, but on a >system such as a laptop, this makes a big >difference.

That depends IMHO on your distribution. CentOS e.g. uses /usr/bin/run-parts to deal with /etc/cron.{hourly,daily,weekly} stuff, which is of course started by /etc/crontab, so no anacron.

mike  on August 28th, 2009

Yeah, I personally like to have things in /var/spool/cron/crontabs with one flat file.

I’ve seen some super nasty crontab files though… This is a more organized approach, but it requires a bit more digging.

natxo asenjo  on August 28th, 2009

mmm, yes, in redhat extensions are allowed with run-parts. With debian and derivatives they’re not (just google run-parts extensions and you will find a lot of people with this ‘problem).

No centos issue, though, so basically, yes you are right :)

Leave a Comment