Skip to main content
RH134

Scheduling Jobs Using Cron

By December 17, 2019September 12th, 2022No Comments

RHCSA 8 Study Guide

For many years Linux administrators have relied on scheduling jobs using cron. The main system configuration file for the crond is /etc/crontab but we also have the extension directory, /etc/cron.d and directories that we can add scripts into for execution, /etc/cron.hourly, /etc/cron.daily and so forth. Users may create their own crontabs with the command crontab -e. The following shows the main /etc/crontab file:

# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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

If we need to schedule a task to run each weekday, Monday to Friday at 15:30 we can add a new file below /etc/crond.d/. Working as root we create the file:

# echo "30 15 * * 1-5 root df -h \
> /tmp/diskfree" > /etc/cron.d/diskfree

To schedule a task to run every 10 minutes to cron file may look similar to this:

# echo "*/10 * * * * root df -h \
> /tmp/diskfree10" > /etc/cron.d/diskfree10

The system cron files include the additional field for the user account context to use when executing the task. As an alternative to scheduling jobs using cron system files, we can also use the crontab command to create and manage user cron jobs which don’t use the user field. Just as with at, we can control access to user crons using the /etc/cron.deny and /etc/cron.allow files. By default only the /etc/cron.deny file exists out of the two files and is empty. An empty deny files then will deny no user accout but we adding a list of names, one per line will deny thoses listed users. Creating the file /etc/cron.allow will grant access only to the listed users in the allow file. An empty /etc/cron.allow file allows access to no user, which is the same as denying acces to all.

The command crontab is used to manage user crontabs. When editing user files we don’t include the user field as the account context is always the owner of the crontab file.

To edit or create a new crontab for your own account:

$ crontab -e

To edit or create a new crontab for another user we work as root:

$ sudo crontab -e -u user1

To list a user crontab we use crontab -l and to delete a user crontab it is crontab -r. The default is to work with the current user but, as root, we can use the option -u to work in the context of another user.

If scheduled jobs are not executed because the system was down it is the crond makes reference to the /etc/anacrontab file. Previous to RHEL 8, this was handled by a separate service but was migrated to crond with the release of RHEL 8. It is also this file that causes the execution of the scripts in the /etc/cron.{time} directories.