Crond Demystified !!

In Linux Fedora Core 1, "crond" is started from the script "/etc/rc.d/init.d/crond". The manual `man cron` says that cron searches "/var/spool/cron" for crontab files which are named after accounts in "/etc/passwd"; crontabs found are loaded into memory. Cron also searches for "/etc/crontab" and the files in the "/etc/cron.d/" directory.

The "/etc/crontab" file contains the following.

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

The "/etc/cron.d" directory contains nothing. So clearly, all system cron are in the /etc/cron.* files. The above commands mean that the program "run-parts" will be run as user "root" for each of the scripts.

The command "run-parts" turns out to be the script "/usr/bin/run-parts", as follows.

#!/bin/bash
 
# run-parts - concept taken from Debian
 
# keep going when something fails
set +e
 
if [ $# -lt 1 ]; then
        echo "Usage: run-parts <dir>"
        exit 1
fi
 
if [ ! -d $1 ]; then
        echo "Not a directory: $1"
        exit 1
fi
 
# Ignore *~ and *, scripts
for i in $1/*[^~,] ; do
        [ -d $i ] && continue
        # Don't run *.{rpmsave,rpmorig,rpmnew,swp} scripts
        [ "${i%.rpmsave}" != "${i}" ] && continue
        [ "${i%.rpmorig}" != "${i}" ] && continue
        [ "${i%.rpmnew}" != "${i}" ] && continue
        [ "${i%.swp}" != "${i}" ] && continue
        [ "${i%,v}" != "${i}" ] && continue
 
        if [ -x $i ]; then
                $i 2>&1 | awk -v "progname=$i" \
                              'progname {
                                   print progname ":\n"
                                   progname="";
                               }
                               { print; }'
        fi
done
 
exit 0

In essence, this just runs all of the scripts in the specified directory. E.g. each hour, the executable plain files in /etc/cron.hourly are run.

See also crontab tutorial.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
job sequence in cron.daily

Hi,
I have created a job in cron.daily for backup. Before backup, i would like to perform logrotate (which is also in cron.daily). However, the job sequence is first backup and then logrotate. I have modified run-parts to list all jobs in cron.daily and the sequence is in alphabetic order (which is what i wanted). However, the job log send to root account indicate a different sequence. Why? How to control the sequence?

(I'm use fedora 2 but the run-parts is just the same as the one posted)
Regards,
Michael

sequencing jobs

Try renaming "logrotate" to "0logrotate". That is a zero prefix.

Sequencing Jobs

Good suggestion. I'll try and see whether it work.

However, i just don't know why the sequence displayed by my test script no match execution sequence.

#!/bin/bash

# run-parts - concept taken from Debian

# keep going when something fails
set +e

if [ $# -lt 1 ]; then
echo "Usage: run-parts dir"
exit 1
fi

if [ ! -d $1 ]; then
echo "Not a directory: $1"
exit 1
fi

# Ignore *~ and *, scripts
for i in $1/*[^~,] ; do
[ -d $i ] && continue
echo "${i}"
done

exit 0

Regards,
Michael

Job Sequence

After modify the script name from logrotate to 0logrotate, logrotate is executed before backup.
Thanks.

Comment