BSD DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Getting Cron to Do Our Bidding
Pages: 1, 2, 3

One final table before continuing with /etc/crontab: You can replace all five time fields with one string value like this:

String

What it Represents

@reboot

only runs the command when you reboot

@yearly

replaces 0 0 1 1 *

@annually

same as yearly

@monthly

replaces 0 0 1 * *

@weekly

replaces 0 0 * * 0

@daily

replaces 0 0 * * *

@midnight

same as daily

@hourly

replaces 0 * * * *

Return to the first virtual terminal to continue reading through /etc/crontab:



# rotate log files every hour, if necessary
0	*	*	*	*	root	newsyslog

Again, the comment is useful: Every minute that is 0 of every hour of every day of every month, run the newsyslog command. And what does the newsyslog command do?

whatis newsyslog

newsyslog(8) - maintain system log files to 
  manageable sizes
  
do daily/weekly/monthly maintenance
59 1 * * * root  periodic daily   2>&1 | sendmail root
30 3 * * 6 root  periodic weekly  2>&1 | sendmail root
30 5 1 * * root  periodic monthly 2>&1 | sendmail root

Notice that the daily/weekly/monthly maintenance scripts use scattered times. The daily script runs every morning at 1:59 A.M.; if you sleep anywhere near your FreeBSD box, you may have heard your hard drive churning at this strange hour. The weekly script runs every Saturday morning at 3:30 A.M., and the monthly script runs on the first day of every month at 5:30 A.M. It is a good thing to run maintenance scripts during times when your computer's processor should be free (e.g., the middle of the night), and to not run the scripts all at the same time to prevent your processor from getting bogged down.

The 2>&1 symbol tells the Bourne shell to save all of the output and error messages together. These are then piped to the sendmail program to be sent to the root account; this means that all output and error messages from running these scripts will be mailed to root as an e-mail message.

Where does cron find the periodic scripts? At your second virtual terminal, try this:

ls /etc/periodic
daily     monthly     weekly 
ls -F /etc/periodic/weekly
300.uucp*     330.catman*       310.locate*		
340.noid*     120.clean-kvmdb*  320.whatis*  
999.local*

The -F to the ls command puts a * after all executables; therefore, /etc/periodic contains subdirectories that contain the scripts that cron runs daily, weekly, and monthly. If you're truly curious, try:

more /etc/periodic/weekly/310.locate

As a side note, if you are learning shell scripting, viewing these built-in scripts provides practice in recognizing proper syntax.

Since the results of these scripts are mailed to root, root can use a mail reader to check for any problems. Normally, you don't configure your favorite e-mail reader for the root account, since you don't want to send and receive e-mail as the root user. However, you do want to be aware of any problems cron finds. Become the superuser and open up your favorite e-mail reader to see the messages sent from cron.

If you've never done this before, you may have hundreds of messages, depending on how long your computer has been turned on. If you always shut off your computer at night, you won't have any, as the scripts all run after midnight.

If you have any messages, you should have at least one called daily run output. If you read this message, you'll see that cron has a lot of work to do every morning at 1:59. You'll also note that the security check was sent in a separate e-mail message called security check output. This message is well worth reading as it contains the results of checking setuid files, uids of 0, passwordless accounts, kernel log messages, failed logins, and refused connections.

Once you've finished reading the messages, you can safely delete them. Don't forget to leave the superuser account when you exit the e-mail reader.

So far, we've looked at the system crontab, which should be left as is. Now we want to look at making your own crontab file to put in the commands that you want cron to execute. By default on a FreeBSD system, any user can create his own crontab file. These crontabs will be stored in /var/cron/tabs. If you:

ls /var/cron/tabs  (must be root to view)

you'll note that this directory is empty by default. Once you create a crontab, it will create a file with the same name as the user. Never edit this file directly using a text editor; instead, use the crontab utility to create or modify your crontab file.

You may be wondering what types of commands or scripts you would want cron to run for you. Ask yourself what types of commands you run regularly, or should run regularly but sometimes forget to run. For example, you may wish to remove old core files or dead.letter files. Perhaps you would like to flush your Netscape cache on a regular basis.

Once you know what you'd like cron to do for you, decide which command or group of commands will accomplish this task. Every entry in a crontab must be one line; It is okay if the line wraps around your screen as long as you don't press enter. If the command is very long, it may be easier to enter the command into a file, or script, and reference the script in crontab. Let's do an example of each.

If I want to find files with certain extensions and remove them, I can enter a command like this (you would enter this all on one line):

find / \( -name "*.core" -or -name "dead.*" \) 
  -print -exec rm -rf {} \;

Let's see if we can interpret this gobblygook. This is all one find statement; find statements always look like this:

find starting_here look_for_this then_do_this

So, when I typed find /, I told find to start looking at root or /.

I then told find to look for files whose names (-name) end with either .core or begin with dead. Because I wanted find to eventually remove both types of these files, I put both names within parentheses. I also had to quote the () with the backslash \ character so the shell wouldn't try to interpret the parentheses.

Once find has found these files, I told it to execute (-exec) the rm -rf command to remove these files. Whenever you use -exec, you must end the find statement with \; or it won't work. The {} tells exec to use whatever find found as the variable to work on.

Pages: 1, 2, 3

Next Pagearrow





Sponsored by: