cron Daemon III: Scheduling Processes
Scheduling Processes
Using cron to Manage Scheduled System Jobs
Using cron to run scheduled system jobs is an extremely useful tool for a Linux system administrator. You can configure your systems to perform a wide variety of tasks on a regular schedule automatically, thus saving you a ton of time and effort.
Remember when I mentioned the problems with running regular backups? Creating a system
job in cron that runs a backup on a regular schedule saves me tons of time. If I don’t have time to
visit a client site on a particular day, I don’t have to worry. I know that cron will run a backup at
the preconfigured time. All I have to do is drop by now and then to verify that everything is still
working properly and to perhaps rotate the backup media. I love cron!
To run system jobs, the cron service uses the
/etc/crontab
file, shown next, to configure what jobs to run:
openSUSE:/etc # cat ./crontab
SHELL=/bin/sh
PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin
MAILTO=root
#
# check scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly
#
-*/15 * * * * root test -x /usr/lib/cron/run-crons &&
/usr/lib/cron/run-crons >/dev/null 2>&1
As you can see in this example, the
/etc/crontab
file contains commands that are used to run scripts found in four different directories:
• /etc/cron.hourly
Contains cron scripts that are run every hour
• /etc/cron.daily
Contains cron scripts that are run every day
• /etc/cron.weekly
Contains cron scripts that are run once a week
• /etc/cron.monthly
Contains cron scripts that are run once a month
All scripts found in any of these directories are automatically run by cron according to the
specified schedule. For example, the /etc/cron.daily directory contains a variety of scripts that are used to clean up your system and rotate your logs once each day. These scripts are shown here:
openSUSE:/etc/cron.daily # ls
logrotate suse-clean_catman suse.de-backup-rpmdb
mdadm suse-do_mandb suse.de-check-battery
packagekit-background.cron suse.de-backup-rc.config suse.de-cron-local
If you have a system task that needs to be run on one of these four schedules, you can simply
create a script file and copy it into the appropriate cron directory in /etc.
What do you do, however, if your system job needs to run on a schedule other than one of the
four used by the cron directories? No problem. The cron daemon has got you covered.
In addition to the four directories just presented, there’s a fifth directory in
/etc/ called cron.d.
If you need a system job to run on a custom schedule, you can create a crontab file in this
directory where it will be read and run by the cron daemon.
How do you create a crontab file? It looks difficult but it really isn’t. A crontab file is simply a text
file that uses one line per job.
Each line has six fields, separated by tabs, as detailed in Table 13-3.
Many times, you will see an asterisk (*) in one or more fields in a given crontab file. This wildcard means “match everything.”
For example, suppose I wanted to run the tar command to back up the /home directory using
the
tar –cvf /media/usb/backup.tar /home
command every day of every month, except Sundays, at 11:05 p.m.
I could create a crontab file in /etc/crontab.d and add the following line:
5 23 * * 1-6 /bin/tar -cvf /media/usb/backup.tar /home
This line in the crontab file specifies that the command be run at 5 minutes after 11:00 p.m.
(23) every day (*) of every month (*) on Monday (1) through Saturday (6).
System cron jobs run as the root user!
[Table 13-3: The crontab File Fields, p 488]
LX0-104 Exam Objectives (L)
No comments:
Post a Comment