Chapter 27. Automating System Tasks
locatecommand is updated daily. A system administrator can use automated tasks to perform periodic backups, monitor the system, run custom scripts, and so on.
27.1. Cron and Anacron
27.1.1. Installing Cron and Anacron
rpm -q cronie cronie-anacroncommand. The command returns full names of the cronie and cronie-anacron packages if already installed or notifies you that the packages are not available.
yumcommand in the following form:
yum install cronie cronie-anacron
root) to run this command. For more information on how to install new packages in Red Hat Enterprise Linux, see Section 8.2.4, “Installing Packages”.
27.1.2. Running the Crond Service
crondservice. This section provides information on how to start, stop, and restart the
crondservice, and shows how to enable it in a particular runlevel. For more information on the concept of runlevels and how to manage system services in Red Hat Enterprise Linux in general, see Chapter 12, Services and Daemons.
126.96.36.199. Starting and Stopping the Cron Service
service crond status.
crondservice in the current session, type the following at a shell prompt as
188.8.131.52. Stopping the Cron Service
crondservice, type the following at a shell prompt as
184.108.40.206. Restarting the Cron Service
crondservice, type the following at a shell prompt:
27.1.3. Configuring Anacron Jobs
/etc/anacrontabfile, which can be only accessed by the root user. The file contains the following:
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=3-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
SHELL— shell environment used for running jobs (in the example, the Bash shell)
PATH— paths to executable programs
MAILTO— user name of the user who receives the output of the anacron jobs by emailIf the
MAILTOvariable is not defined (
MAILTO=), the email is not sent.
RANDOM_DELAY— maximum number of minutes that will be added to the
delay in minutesvariable which is specified for each jobThe minimum delay value is set, by default, to 6 minutes.If
RANDOM_DELAYis, for example, set to
12, then between 6 and 12 minutes are added to the
delay in minutesfor each job in that particular anacrontab.
RANDOM_DELAYcan also be set to a value below
0. When set to
0, no random delay is added. This proves to be useful when, for example, more computers that share one network connection need to download the same data every day.
START_HOURS_RANGE— interval, when scheduled jobs can be run, in hoursIn case the time interval is missed, for example due to a power failure, the scheduled jobs are not executed that day.
/etc/anacrontabfile represent scheduled jobs and follow this format:
period in days delay in minutes job-identifier command
period in days— frequency of job execution in daysThe property value can be defined as an integer or a macro (
@dailydenotes the same value as integer 1,
@weeklythe same as 7, and
@monthlyspecifies that the job is run once a month regarless of the length of the month.
delay in minutes— number of minutes anacron waits before executing the jobThe property value is defined as an integer. If the value is set to
0, no delay applies.
job-identifier— unique name referring to a particular job used in the log files
command— command to be executedThe command can be either a command such as
ls /proc >> /tmp/procor a command which executes a custom script.
220.127.116.11. Examples of Anacron Jobs
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=30 # the jobs will be started during the following hours only START_HOURS_RANGE=16-20 #period in days delay in minutes job-identifier command 1 20 dailyjob nice run-parts /etc/cron.daily 7 25 weeklyjob /etc/weeklyjob.bash @monthly 45 monthlyjob ls /proc >> /tmp/proc
anacrontabfile are randomly delayed by 6-30 minutes and can be executed between 16:00 and 20:00.
/etc/cron.dailydirectory using the
run-partsscripts accepts a directory as a command-line argument and sequentially executes every program in the directory).
weeklyjob.bashscript in the
/etcdirectory once a week.
ls /proc >> /tmp/proc) once a month.
27.1.4. Configuring Cron Jobs
/etc/crontab, which can be only modified by the root user. The file contains the following:
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # 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 # | | | | | # * * * * * username command to be executed
MAILTO. For more information about these variables, see Section 27.1.3, “Configuring Anacron Jobs”.
HOMEvariable defines the directory, which will be used as the home directory when executing commands or scripts run by the job.
/etc/crontabfile represent scheduled jobs and have the following format:
minute hour day month day of week username command
minute— any integer from 0 to 59
hour— any integer from 0 to 23
day— any integer from 1 to 31 (must be a valid day if a month is specified)
month— any integer from 1 to 12 (or the short name of the month such as jan or feb)
day of week— any integer from 0 to 7, where 0 or 7 represents Sunday (or the short name of the week such as sun or mon)
username— specifies the user under which the jobs are run
command— the command to be executedThe command can be either a command such as
ls /proc /tmp/procor a command which executes a custom script.
1-4means the integers 1, 2, 3, and 4.
3, 4, 6, 8indicates exactly these four integers.
/integer. For example, minute value defined as
0-59/2denotes every other minute in the minute field. Step values can also be used with an asterisk. For instance, if the month value is defined as
*/3, the task will run every third month.
crontabutility. The user-defined crontabs are stored in the
/var/spool/cron/directory and executed as if run by the users that created them.
crontab -eto edit the user's crontab with the editor specified in the
EDITORenvironment variable. The file uses the same format as
/etc/crontab. When the changes to the crontab are saved, the crontab is stored according to user name and written to the file
/var/spool/cron/username. To list the contents of your crontab file, use the
/etc/cron.d/directory contains files that have the same syntax as the
/etc/crontabfile. Only root is allowed to create and modify files in this directory.
/etc/cron.d/directory, and the
/var/spool/cron/directory every minute for changes and the detected changes are loaded into memory. It is therefore not necessary to restart the daemon after an anacrontab or a crontab file have been changed.
27.1.5. Controlling Access to Cron
/etc/cron.denyfiles. These access control files use the same format with one user name on each line. Mind that no whitespace characters are permitted in either file.
cron.allowfile exists, only users listed in the file are allowed to use cron, and the
cron.denyfile is ignored.
cron.allowfile does not exist, users listed in the
cron.denyfile are not allowed to use Cron.
crond) does not have to be restarted if the access control files are modified. The access control files are checked each time a user tries to add or delete a cron job.
/etc/security/access.conffile. For example, after adding the following line to the file, no other user but the root user can create crontabs:
-:ALL EXCEPT root :cron
man 5 access.conf).
27.1.6. Black and White Listing of Cron Jobs
/etc/cron.daily: if the user adds programs located in the directory to the job black list, the run-parts script will not execute these programs.
jobs.denyfile in the directory that
run-partsscripts will be executing from. For example, if you need to omit a particular program from
/etc/cron.daily, create the
/etc/cron.daily/jobs.denyfile. In this file, specify the names of the programs to be omitted from execution (only programs located in the same directory can be enlisted). If a job runs a command which runs the programs from the cron.daily directory, such as
run-parts /etc/cron.daily, the programs defined in the
jobs.denyfile will not be executed.
jobs.alloware the same as those of
cron.allowdescribed in section Section 27.1.5, “Controlling Access to Cron”.