You are on page 1of 16

Chapter – 10

Automated Tasks
Objectives:

At the end of this module, you would have gained


fair knowledge on:

•Scheduling jobs
•Configuring at, cron and anacron
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 Linux comes preconfigured 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 Linux comes with four automated


tasks utilities: cron, anacron, at, and batch.
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 configure tasks based on time periods instead of exact times,
refer to the Section called Anacron. To schedule one-time tasks,
refer to the Section called At and Batch.

To use the cron service, you must have the vixie-


cron RPM package 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.
Configuring Cron Tasks
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
Each line in the /etc/crontab file represents a task
and has the format:
minute hour day month
dayofweek 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, feb, and so on)

dayofweek — any integer from 0 to 7 where 0 or 7 represents


Sunday (or the short name of the week such as sun, mon, and so on)
Examples of crontabs

# 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
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. White space 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
user names listed in the access control files.
If the file cron.allow exists, only users listed in it are allowed to use
cron, and the at.deny file is ignored.
If cron.allow does not exist, all users listed in
cron.deny are not allowed to use cron.
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
Anacron
Anacron is a task scheduler similar to
cron except that it does not require the system to
run continuously. It can be used to run the daily,
weekly, and monthly jobs usually run by cron.

To use the Anacron service, you must


have the anacron RPM package installed and the
anacron service must be running.

To determine if the package is


installed, use the rpm -q anacron command. To
determine if the service is running, use the
command /sbin/service anacron status
Configuring Anacron Tasks
Anacron tasks are listed in the configuration file
/etc/anacrontab. Each line in the configuration file corresponds to
a task and has the format:
period delay job-identifier
command
period — frequency (in days) to execute the
command

delay — delay time in minutes

job-identifier — description of the task, used in


Anacron messages and as the name of the job's
At and Batch
While cron and anacron are used to
schedule recurring tasks, the at command is used
to schedule a one-time task at a specific time. The
batch command is used to schedule a one-time
task to be executed when the systems load average
drops below 0.8.

To use at or batch you must have the at


RPM package 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.
Configuring At Jobs

To schedule a one-time 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:

HH:MM format — For example, 04:00 specifies 4:00AM.


If the time is already pasted, it is executed at the specified
time the next day.

midnight — Specifies 12:00AM.

noon — Specifies 12:00PM.


Configuring Batch Jobs
To execute a one-time 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.

More than one command 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.
Viewing Pending Jobs
To view pending at and batch jobs, use
the atq command. It displays a list of pending jobs,
with each job on a line.

Each line is in the format job number,


date, hour, job class, and username. Users can
only view their own jobs. If the root user executes
the atq command, all jobs for all users are
displayed.
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