-->

Previous | Table of Contents | Next

Page 347

Hour 24

Scheduling

In this hour, you'll finish with learning system administration skills. By now you've learned most of the commands used by sysadmins, and should be familiar with most of the tasks you have to perform to maintain your system for yourself or other users. This hour shows you how to put all this knowledge together in order to automate these tasks using the cron daemon, and other Linux scheduling programs.

By using the programs and techniques outlined in this hour, you can automate many different system administration jobs and maintain a healthy, well-running system. The first topic discussed is the cron daemon, and then you'll learn how to administer the at command facilities for different users on your system.

Using the cron Daemon

The cron daemon, crond, is a program started after you boot Linux by the cron.init script in the /etc/rc.d/init.d directory on your system. This is done automatically, so you don't have to worry about starting the cron daemon every time after your

Page 348

boot Linux. The crond program runs in the background, and checks several files. The first is the crontab file in the /etc directory. A portion of this file reads as follows:

...

# run-parts

01 * * * * root run-parts /etc/cron.hourly

02 1 * * * root run-parts /etc/cron.daily

02 2 * * 0 root run-parts /etc/cron.weekly

02 3 1 * * root run-parts /etc/cron.monthly

...

As you can see, there is a list of files (actually, directories), which contain tasks that are run hourly, daily, weekly, and monthly. If you look at the contents of the cron.weekly directory, you'll find a file called makewhatis.cron that contains


#!/bin/bash



makewhatis -w

exit 0

You can see that this is a shell script that executes the makewhatis command to build your system's whatis database (see Hour 4, "Reading and Navigation Commands").

The cron command also searches the /var/spool/cron directory for personal crontab files with user's names. These files are created with the crontab command, found under the /usr/bin directory, and are used by users to schedule their own regular tasks. But how do you tell cron when to run these scripts? Read on to see the format of the cron commands.

Managing User cron Scheduling

Although the /etc/crontab file is for scheduling regular, system-wide tasks, you can let users on your system create their own cron schedules. If you want to enable your users to use the crontab command to create personal cron files under the /var/spool/cron directory, you should first create two files: /etc/cron.allow and /etc/cron.deny. Under the /etc/cron.allow file, insert the root operator name, root, and the names of any users you want to allow access to the cron daemon. If neither of these files exist, users may or may not have access to personal cron files. You can create files for your users with the crontab command, or users may create their own.

CAUTION
Be careful! Always use the crontab command's -u command-line option. If you run this while running as root (after using the su command), and don't use this option, you'll edit the root operator's crontab settings instead of your own.

The next section shows you the format of the crontab file, and the difference between the format of a cron entry for your Linux system and for individual users.

Setting Schedules with the crontab Command

Page 349

The format of crontab entries is detailed in the crontab manual page under section 5. To see the manual page, use


# man 5 crontab

This page provides specifications for crontab entries. However, to make things simpler, I'll give you some examples, and at the end of this hour, some samples you can use. In general, the fields of an entry are


minutes    hour    day of month    month    day of week    command

Entries are usually separated with a space. However, cron entries in the /etc/crontab file must have a username inserted between the day of week entry and the command. A username field is not needed for personal crontab entries.

As a simple example, you can have Linux tell you the time every 15 minutes by first calling the crontab command with the -e command-line option, and then adding


0,15,30,45 * * * * /usr/local/bin/saytime %

This tells the cron daemon to execute the saytime command to speak the time every fifteen minutes. You can find the saytime command at




http://sunsite.unc.edu/pub/Linux/sound/speech/saytime.tgz

If you'd like to hear the time every minute (though it might drive you crazy!), you can use


* * * * * /usr/local/bin/saytime %

Note that if you don't use a carriage-return at the end of the line in your entry, you should use a percent (%) sign. Here are some more example entries:


* 22 * * 1-4 /usr/bin/wall `Time for bed! Finished your homework yet?' %

0 1 * * * /usr/bin/find / -xdev -name core -exec /bin/rm {} \; %

0,30 * * * /usr/bin/tput bel >/dev/console %

* 12 25 12 * /bin/echo `Happy Holidays' | /bin/mail -s Greetings root %

The first example broadcasts a gentle reminder to all your users using the wall command, at 10 p.m., Monday through Thursday. The second example runs the find command to search your system at 1 a.m. each morning for core files, and deletes any found. The third rings your terminal's bell on the hour and half hour, using the tput command to output. The last example sends a mail message at noon on December 25th.

Experiment with different tasks and times. You can also have search results for files, reports of users online, and uptime reports directed to log files, or mailed to you. You can also use cron to schedule backups when you're away, or have your system shut down at a preselected time.

Previous | Table of Contents | Next