Now
that you understand the basics of automating tasks with a script or program,
the next step is to know how to schedule the tasks so they are executed at a
specific time or on a set schedule. Some scripts such as removing users may not
need to be scheduled, but others such as performing backups might work better
on a schedule so users can anticipate them or so they can be run during a time
when they won’t interfere with the daily workload of the system.
Your
Red Hat Enterprise Linux system should have the packages called vixie-cron and crontabs
installed by default because basic system maintenance such as rotating log
files is automated through the cron daemon.
The
vixie-cron package installs a daemon called crond. This daemon references a set
of files that contains lists of tasks to run every hour, every day, every week,
every month, or at a specific time. If a specific time is listed, the minute,
hour, day of the month, month, and day of the week can be scheduled. It also
provides the initialization script used to start the daemon at boot time and
the crontab executable used by non-root users to schedule cron tasks.
The
crontabs package sets up the basic directory structure for the cron files. The
following directories are created:
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
Along
with these directories, the /etc/crontab and the /usr/bin/run-parts files are
installed.
The /etc/crontab file defines the SHELL, PATH, MAILTO, and HOME variables and then
defines when to run the hourly, daily, weekly, and monthly cron tasks. The /usr/bin/run-parts
file is a bash script called by the /etc/crontab file to run the tasks in the
hourly, daily, weekly, and monthly cron directories.
Because
you have root privileges to the system as the administrator, you can add your custom
script to one of the directories set up by the crontabs package or add a
specially formatted file in the /etc/cron.d/ directory.
After
adding your custom script to the appropriate directory, it is executed when the
tasks for each directory are scheduled to run as defined in /etc/crontab.
To
use a specially formatted file in the /etc/cron.d/ directory instead, create a
file in the directory with a unique, descriptive name such as backup for a cron
task that creates backup. For example, to echo a message to a file 47 minutes
after every hour, create a file called testing containing the following:
47 * * * * root echo “testing from
cron.d” >> /tmp/testing
The
contents of each file in the /etc/cron.d/ directory must use the following
format:
*
* * * * username command
The
five asterisks should be replaced by the minute, hour, day of the month, month,
and day of the week on which to execute the command.
The
following entry in /var/log/cron confirms the addition and the execution of the
task:
May
13 15:37:01 goofy crond[1824]: (*system*) RELOAD (/etc/cron.d/testing)
May
13 15:47:01 goofy crond[6977]: (root) CMD (echo “testing from cron.d” >>
/tmp/testing)
Finally,
if you want to add a cron task as a user, execute the crontab -e command as a non-root
user. This utility allows each user to have his or her own list of cron tasks.
For example, suppose the user tfox executes crontab -e and enters the content
from Listing 11.12. Note that the default editor used is Vi.
If you have any question or comments please let us know at Sallar
0 comments:
Post a Comment