UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 21.7 Setting File Modification Time with touch Chapter 21
More About Managing Files
Next: 21.9 Keep File Printouts Up-to-Date Automatically with make
 

21.8 The MAILCHECK and mail Variables Check More than Mail

Depending on how your system is set up, you may notice that it periodically says something like You have new mail. When you run your mail program (1.33), the mail will be waiting for you in your mailbox. You can also use this feature to check for changes in several mailboxes, as well as changes in files and directories that don't hold mail - more about that in a minute.

21.8.1 For C Shell Users

If you use the C shell, this feature is controlled by the mail shell variable (usually set in your .cshrc file (2.3)).

The shell normally checks your mailbox every five minutes. However, you can set a different interval at the start of the list. For example, the command below tells the shell to check my mailbox every 60 seconds:

% set mail=(60 /usr/spool/mail/mikel)

Note that the exact filename depends upon how your mail system is set up. For example, many systems use /usr/mail instead of /usr/spool/mail. Checking for mail takes time and can delay your prompt on busy systems. Don't set a short interval unless you need to.

21.8.1.1 Multiple Mailboxes

Many users need to watch more than one mailbox. For example, I need to watch /usr/spool/mail/mikel; but if I'm responsible for product support, and my company maintains a special mail ID for support questions, I might also want to watch /usr/spool/mail/prodsupport. To do this, we set the mail variable so that it's a list of important files and directories:

% set mail=(/usr/spool/mail/mikel /usr/spool/mail/prodsupport)

When the list has more than one file, the shell will tell you which file has changed with a message like new mail in /usr/spool/mail/prodsupport.

21.8.1.2 Watching Other Files

All mail is doing is looking to see whether or not the file has changed; it doesn't know that it's looking at a "mail" file. Therefore, you can use it to watch anything you want; your list can even include directories. For example, let's say that you're running a program that periodically writes to the file /home/los/mikel/radio/log.out. Then you can set mail as follows:

% set mail=(/home/los/mikel/radio/log.out other-files)

21.8.1.3 Watching Directories

Watching a directory is the same as watching a file; you'll be notified whenever the directory changes (whenever a file is added or deleted in the directory). So let's modify our previous example slightly; let's say that your reports are named /home/los/mikel/radio/log/date, where the date indicates when the report was created. Every report thus generates a new file. In this case, you'd want to watch the log directory for the creation of new files.

% set mail=(/home/los/mikel/radio/log other-files)

Here's another example. Let's say that you suspect someone is using UUCP (1.33) to send company secrets to a system named somewhere. You want to watch this systems's UUCP traffic very carefully. To do so, you can tell the shell to inform you whenever the logfile changes:

% set mail=(5 /usr/spool/uucp/.Log/uucico/somewhere)

We've told the shell to check the log every five seconds because, given that we suspect security problems, we want to get our reports immediately.

If the directory you're watching is actually a symbolic link (18.4) to another directory, be sure to check the actual directory and not the link. The sl (18.8) script is handy for this - or you can use ls -ld (16.8):

% ls -ld /usr/local/logs
lrwxrwxrwx  1 root    15 Jul 10  1990 /usr/local/logs -> /foo/bar/logs
% ls -ld /foo/bar/logs
drwxrwxr-x  2 root  512 Aug 10 12:20 /foo/bar/logs
% set mail=(/foo/bar/logs)

21.8.2 For Bourne Shell Users

Now, let's assume that you're a Bourne shell user, and go through everything once more. The Bourne shell uses three variables to control mail notification. (These are usually set in users' .profile (2.2) files. To make them work in subshells (38.4), export (6.1) the variables.) We'll assume that you read the C shell description already given, and move a bit faster.

First, if you want to check only one file or directory, set the variable MAIL accordingly.

$ MAIL=/usr/spool/mail/mikel
$ export MAIL

NOTE: The next three features don't work on some Bourne shells.

By default, the Bourne shell checks every ten minutes. To check at some other interval, set the variable MAILCHECK to your new interval, in seconds: for example, the command below tells the shell to check every 55 seconds:

$ MAILCHECK=55

One useful trick: if you set MAILCHECK to 0, the shell will check whenever it prints the "primary" prompt (by default, $). In other words, it will check after each command. This may be slow on busy systems.

If you want to watch several files, use the MAILPATH variable. Its value must be a list of file or directory names, separated by colons. For example:

$ MAILPATH=/usr/spool/mail/mikel:/usr/spool/mail/prodsupport

If MAILPATH is set, the shell will ignore the MAIL variable. You can't use both.

Normally, the Bourne shell prints you have mail whenever any file that it's watching changes. However, if you follow a filename in MAILPATH with a percent sign (%) and a message, the shell will print the message whenever the file changes. For example, let's have the shell print you have mail when mail comes in and New log! when a log file changes:

$ MAILPATH=/usr/spool/mail/mikel:/home/mikel/Z/log%"New log!"

You can create a different message for every file that you care about. Note that the Korn shell and bash use ? (a question mark) instead of the % before each message.

- ML


Previous: 21.7 Setting File Modification Time with touch UNIX Power ToolsNext: 21.9 Keep File Printouts Up-to-Date Automatically with make
21.7 Setting File Modification Time with touch Book Index21.9 Keep File Printouts Up-to-Date Automatically with make

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System