red hat enterprise linux 4: system administration guide p rev
n ext
chapter 27. automated tasks in linux, tasks can be configured to run automatically within a specified period of time, on a specified date, or when the system load average is below a specified number. red hat enterprise linux is pre configured to run important system tasks to keep the system updated. for example, the slocate database used by the locate command is updated daily. a system
administrator can use automated tasks to perform periodic backups, monitor the system, run custom scripts, and more. red hat enterprise linux comes with
several automated tasks utilities: cron,
at, and batch. 27.1. cron
cron is a daemon that can be used to schedule the execution of recurring tasks according to a combination of the time, day of the month, month, day of the week, and week. cron assumes that the system is on continuously. if the system is not on when a task is scheduled, it is not executed. to schedule onetime tasks, refer to section 27.2 at and batch. to use the cron service, the vixiecron rpm package must be installed and the crond service must be
running. to determine if the package is installed, use the rpm -q vixie-
cron command. to determine if the
service is running, use the command /sbin/service crond status. 27.1.1. configuring cron tasks the main configuration file for cron, /etc/crontab, contains the following lines:
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 first four lines are variables used
to configure the environment in which the cron tasks are run. the shell variable tells the system which shell environment to use (in this example the bash shell), while the path variable defines the path used to
execute commands. the output of the cron tasks are emailed to the username defined with the mailto variable. if the mailto variable is
defined as an empty string (mailto=""), email is not sent. the
home variable can be used to set the
home directory to use when executing commands or scripts. each line in the /etc/crontab file represents a task and has the following format:
minute hour day dayofweek command
month
7
minute — any integer from 0 to 59
8
hour — any integer from 0 to 23
9
day — any integer from 1 to 31
(must be a valid day if a month is specified) 10
month — any integer from 1 to 12 (or the short name of the month such as jan or feb)
11
dayofweek — any integer from 0 to 7, where 0 or 7 represents sunday (or the short name of the week such as sun or mon)
12
command — the command to
execute (the command can either be a command such as ls /proc
>> /tmp/proc or the command to execute a custom script)
for any of the above values, an
asterisk (*) can be used to specify all valid values. for example, an asterisk for the month value means execute the command every month within the constraints of the other values. a hyphen () between integers specifies a range of integers. for example, 1-4 means the integers 1, 2, 3, and 4.
a list of values separated by commas (,) specifies a list. for example, 3, 4,
6, 8 indicates those four specific integers.
the forward slash (/) can be used to specify step values. the value of an integer can be skipped within a range by following the range with
/. for example, 0-59/2 can be used to define every other minute
in the minute field. step values can also be used with an asterisk. for instance, the value */3 can be used in the month field to run the task every third month.
any lines that begin with a hash mark (#) are comments and are not processed. as shown in the /etc/crontab file, the run-parts script executes the
scripts in the /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/ directories on an hourly, daily, weekly, or monthly basis respectively. the files in these directories should be shell scripts. if a cron task is required to be executed on a schedule other than
hourly, daily, weekly, or monthly, it can be added to the /etc/cron.d/ directory. all files in this directory use the same syntax as /etc/crontab. refer to example 271 for examples.
# record the memory usage of the system every monday # at 3:30am in the file /tmp/meminfo 30 3 * * mon cat /proc/meminfo >> /tmp/meminfo # run custom script the first day of every month at 4:10am 10 4 1 * * /root/scripts/backup.sh example 271. crontab examples users other than root can configure cron tasks by using the crontab
utility. all userdefined crontabs are stored in the /var/spool/cron/ directory and are executed using the usernames of the users that created
them. to create a crontab as a user, login as that user and type the command crontab -e to edit the
user's crontab using the editor specified by the visual or editor
environment variable. the file uses the same format as /etc/crontab. when the changes to the crontab are saved, the crontab is stored according to username and written to the file /var/spool/cron/username. the cron daemon checks the /etc/crontab file, the /etc/cron.d/ directory, and the /var/spool/cron/ directory every minute for any changes. if any changes are found, they are loaded into memory. thus, the daemon does not need to be restarted if a crontab file is changed. 27.1.2. controlling access to cron
the /etc/cron.allow and /etc/cron.deny files are used to restrict access to cron. the format of both access control files is one username on each line. whitespace is not permitted in either file. the cron daemon (crond) does not have to be
restarted if the access control files are modified. the access control files are read each time a user tries to add or delete a cron task. the root user can always use cron, regardless of the usernames listed in the access control files. if the file cron.allow exists, only users listed in it are allowed to use cron, and the cron.deny file is ignored. if cron.allow does not exist, users listed in cron.deny are not allowed to
use cron. 27.1.3. starting and stopping the service to start the cron service, use the command /sbin/service crond
start. to stop the service, use the command /sbin/service crond
stop. it is recommended that you start the service at boot time. refer to chapter 12 controlling access to
services for details on starting the cron service automatically at boot time.
prev
home
additional
up
resources red hat enterprise linux 4: system
next at and batch
administration guide n pr chapter 27. ex ev automated tasks t 27.2. at and batch while cron is used to schedule recurring tasks, the at command is used to schedule a onetime task at a specific
time and the batch command is used to
schedule a onetime task to be executed when the systems load average drops below 0.8. to use at or batch, the at rpm package must be installed, and the atd service must be running. to determine if the
package is installed, use the rpm -q at
command. to determine if the service is running, use the command
/sbin/service atd status. 27.2.1. configuring at jobs to schedule a onetime job at a specific time, type the command at time, where
time is the time to execute the command. the argument time can be one of the following: 13hh:mm format — for example, 04:00 specifies 4:00 a.m. if the time is already past, it is executed at the specified time the next day. 14midnight — specifies 12:00 a.m. 15noon — specifies 12:00 p.m. 16teatime — specifies 4:00 p.m. 17monthname day year format — for example, january 15 2002 specifies the 15th day of january in the year
2002. the year is optional. 18mmddyy, mm/dd/yy, or mm.dd.yy formats — for example, 011502 for the 15th day of january in the year 2002. 19now + time — time is in minutes, hours, days, or weeks. for example, now + 5 days specifies that the command should be executed at the same time five days from now. the time must be specified first, followed by the optional date. for more information about the time format, read the /usr/share/doc/at/timespec text file. after typing the at command with the
time argument, the at> prompt is displayed. type the command to execute, press [enter], and type [ctrl][d]. multiple commands can be specified by typing
each command followed by the [enter] key. after typing all the commands, press [enter] to go to a blank line and type [ctrl][d]. alternatively, a shell script can be entered at the prompt, pressing [enter] after each line in the script, and typing [ctrl][d] on a blank line to exit. if a script is entered, the shell used is the shell set in the user's shell environment, the user's login shell, or /bin/sh (whichever is found first).
if the set of commands or script tries to display information to standard out, the output is emailed to the user. use the command atq to view pending jobs. refer to section 27.2.3 viewing pending jobs for more information. usage of the at command can be
restricted. for more information, refer to
section 27.2.5 controlling access to at and batch for details. 27.2.2. configuring batch jobs to execute a onetime task when the load average is below 0.8, use the batch command.
after typing the batch command, the at> prompt is displayed. type the command
to execute, press [enter], and type [ctrl] [d]. multiple commands can be specified by typing each command followed by the [enter] key. after typing all the commands, press [enter] to go to a blank line and type [ctrl][d]. alternatively, a shell script can be entered at the prompt, pressing [enter] after each line in the script, and typing [ctrl][d] on a blank line to exit. if a script is entered, the shell used is the shell set in the user's shell environment, the user's login shell, or
/bin/sh (whichever is found first). as
soon as the load average is below 0.8, the set of commands or script is executed. if the set of commands or script tries to display information to standard out, the output is emailed to the user. use the command atq to view pending jobs. refer to section 27.2.3 viewing pending jobs for more information. usage of the batch command can be
restricted. for more information, refer to section 27.2.5 controlling access to at and batch for details. 27.2.3. viewing pending jobs to view pending at and batch jobs, use the atq command. the atq command
displays a list of pending jobs, with each job on a line. each line follows the job number, date, hour, job class, and
username format. users can only view their own jobs. if the root user executes the atq command, all jobs for all users are displayed.
27.2.4. additional command line options additional command line options for at and batch include: option
description
-f
read the commands or shell script from a file instead of specifying them at the prompt.
-m
send email to the user when the job has been completed.
-v
display the time that the job is executed.
table 271. at and batch command line options
27.2.5. controlling access to at and batch the /etc/at.allow and /etc/at.deny files can be used to restrict access to the at and batch commands. the format of
both access control files is one username on each line. whitespace is not permitted in either file. the at daemon (atd) does not have to be restarted if the access
control files are modified. the access control files are read each time a user tries to execute the at or batch commands.
the root user can always execute at and
batch commands, regardless of the access control files.
if the file at.allow exists, only users listed in it are allowed to use at or batch, and the at.deny file is ignored.
if at.allow does not exist, users listed in
at.deny are not allowed to use at or
batch.
27.2.6. starting and stopping the service to start the at service, use the command
/sbin/service atd start. to stop the
service, use the command /sbin/service
atd stop. it is recommended that you start the service at boot time. refer to
chapter 12 controlling access to services for details on starting the cron service automatically at boot time.
prev automated tasks
home up
next additional resources