Scheduling system maintenance cron jobs in CentOS/RHEL 6.x

CentOS 6.x has changed how the default system maintenance cronjobs are scheduled. These are the cron jobs responsible for things like rotating logs and indexing files on the filesystem. That is routine jobs that are best scheduled to run at off peak times when the server is not busy doing more important things like serving money making websites. So how do you go about changing the timing of cron jobs on CentOS or RHEL 6.x?

First, a look at how the jobs are scheduled. We just follow the trail:

  • Previously, in CentOS/RHEL 5.x system maintenance cronjobs were scheduled using the /etc/crontab file. This is no longer the case in CentOS 6.x. /etc/crontab is empty by default.
  • Anacron is installed by default and is responsible for maintenance cron jobs. Anacron was originally created for running cron jobs on systems that aren’t switched on 24/7 i.e. desktop or workstation PCs. So why Redhat chose to include it by default in an enterprise operating system designed for servers that are always on is anyone’s guess.
  • Anacron is run by crond via the 0anacron file in /etc/cron.hourly. Anacron does NOT run as it’s own daemon.
  • Anacron is configured using /etc/anacrontab and executes the commands in the cron.daily/weekly/monthly directories:
    SHELL=/bin/sh
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    # the maximal random delay added to the base delay of the jobs
    RANDOM_DELAY=45
    # the jobs will be started during the following hours only
    START_HOURS_RANGE=20-22
     
    #period in days   delay in minutes   job-identifier   command
    1       5       cron.daily              nice run-parts /etc/cron.daily
    7       25      cron.weekly             nice run-parts /etc/cron.weekly
    @monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

So if you want to schedule system maintenance cron jobs to run at off-peak times you will have to tweak the START_HOURS_RANGE in /etc/anacrontab.

Or if you want more fine grained control you could just ditch anacron altogether and use /etc/cron.d/dailyjobs 🙂

yum remove cronie-anacron
yum install cronie-noanacron sysstat

Don’t forget to start crond!

service crond start
chkconfig crond on

6 thoughts on “Scheduling system maintenance cron jobs in CentOS/RHEL 6.x

  1. Thanks super useful.
    I went a small step more.

    I don’t like the /etc/cron.d/dailyjobs as that is different than RHEL/Centos 5, so I removed it and /etc/cron.d/0hourly. Then put the default entries (& mine) into /etc/crontab:

    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

    Did it so I can see everything in 1 spot.

  2. Some more info, once you install the normal cron, as root do:

    crontab -e

    This will open up an editor where you can add your jobs. Save the file and it will install a new crontab for you. To confirm this, do the following to list the current crontab:

    crontab -l

    • The commands you listed can be used by any user to modify his personal crontab. They are not used to modify the operating system’s cron jobs.

Leave a Reply

Your email address will not be published. Required fields are marked *