Bookmark this page

Scheduling System cron Jobs

  • System crontabs have an extra column: Username.

  • System crontab files in /etc/crontab and /etc/cron.d/*.

  • Scripts controlled by /etc/anacrontab in /etc/cron.{hourly,daily,weekly,monthly}/.

Objectives

After completing this section, students should be able to:

  • Schedule recurring system tasks.

Scheduling recurring system jobs with cron

System cron jobs

Apart from user cron jobs, there are also system cron jobs.

System cron jobs are not defined using the crontab command, but are instead configured in a set of configuration files. The main difference in these configuration files is an extra field, located between the Day-of-Week field and the Command field, specifying under which user a job should be run.

The /etc/crontab has a useful syntax diagram in the included comments.

 # For details see man 4 crontabs

# Example of job definition:
# .---------------- 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
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

System cron jobs are defined in two locations: /etc/crontab and /etc/cron.d/*. Packages that install cron jobs should do so by placing a file in /etc/cron.d/, but administrators can also use this location to more easily group related jobs into a single file, or to push jobs using a configuration management system.

There are also predefined jobs that run every hour, day, week, and month. These jobs will execute all scripts placed in /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/ respectively. Please note that these directories contain executable scripts, and not cron configuration files.

Note

Make sure to make any scripts you place in these directories executable. If a script is not made executable (e.g., with chmod +x), it will not be run.

The /etc/cron.hourly/* scripts are executed using the run-parts command, from a job defined in /etc/cron.d/0hourly. The daily, weekly, and monthly jobs are also executed using the run-parts command, but from a different configuration file: /etc/anacrontab.

In the past, /etc/anacrontab was handled by a separate daemon (anacron), but in Red Hat Enterprise Linux 7, the file is parsed by the regular crond daemon. The purpose of this file is to make sure that important jobs will always be run, and not skipped accidentally because the system was turned off or hibernating when the job should have been executed.

The syntax of /etc/anacrontab is different from the other cron configuration files. It contains exactly four fields per line:

  • Period in days

    Once per how many days this job should be run.

  • Delay in minutes

    The amount of time the cron daemon should wait before starting this job.

  • Job identifier

    This is the name of the file in /var/spool/anacron/ that will be used to check if this job has run. When cron starts a job from /etc/anacrontab, it will update the timestamp on this file. The same timestamp is used to check when a job has last run in the past.

  • Command

    The command to be executed

/etc/anacrontab also contains environment variable declarations using the syntax NAME=value. Of special interest is START_HOURS_RANGE: Jobs will not be started outside of this range.

References

crond(8), crontab(1), and crontab(5), anacron(8), and anacrontab(5) man pages

Revision: rh134-7-63a207e