12.07.2015 Views

Red Hat Enterprise Linux 5 Administration Unleashed

Red Hat Enterprise Linux 5 Administration Unleashed

Red Hat Enterprise Linux 5 Administration Unleashed

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

250CHAPTER 11Automating Tasks with Scriptsothers such as performing backups might work better on a schedule so users can anticipatethem or so they can be run during a time when they won’t interfere with the dailyworkload of the system.Your <strong>Red</strong> <strong>Hat</strong> <strong>Enterprise</strong> <strong>Linux</strong> system should have the packages called vixie-cron andcrontabs installed by default because basic system maintenance such as rotating log filesis automated through the cron daemon.The vixie-cron package installs a daemon called crond. This daemon references a set offiles that contains lists of tasks to run every hour, every day, every week, every month, orat 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 tostart the daemon at boot time and the crontab executable used by non-root users toschedule cron tasks.The crontabs package sets up the basic directory structure for the cron files. The followingdirectories are created:/etc/cron.daily/etc/cron.hourly/etc/cron.monthly/etc/cron.weeklyAlong with these directories, the /etc/crontab and the /usr/bin/run-parts files areinstalled. The /etc/crontab file defines the SHELL, PATH, MAILTO, and HOME variables andthen 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 inthe hourly, daily, weekly, and monthly cron directories.Because you have root privileges to the system as the administrator, you can add yourcustom script to one of the directories set up by the crontabs package or add a speciallyformatted file in the /etc/cron.d/ directory.After adding your custom script to the appropriate directory, it is executed when the tasksfor each directory are scheduled to run as defined in /etc/crontab. The following logentry in /var/log/cron confirms that the daemon executed the daily scripts (thisexample was added to /etc/cron.daily/):May 15 04:02:01 goofy crond[447]: (root) CMD (run-parts /etc/cron.daily)To use a specially formatted file in the /etc/cron.d/ directory instead, create a file in thedirectory with a unique, descriptive name such as backup for a cron task that createsbackup. For example, to echo a message to a file 47 minutes after every hour, create a filecalled testing containing the following:47 * * * * root echo “testing from cron.d” >> /tmp/testingThe contents of each file in the /etc/cron.d/ directory must use the following format:* * * * * username command

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!