Now, that wasn't so bad, was it? Let's get even fancier and create a simple shell script to clear the Netscape cache. This script was borrowed from the FreeBSD mailing list archives -- from this message and also this one.

Let's pick apart what we just typed in here. All scripts begin with a
shebang:

#!

which is followed by the full pathname to the program that will interpret the script. We've just created a Bourne shell script as we've indicated that the sh command (the Bourne shell) will be responsible for interpreting this script with this line:

#!/bin/sh

We then included four comment lines (they begin with the # character) to remind ourselves of the purpose of this script.

Then we get to the actual meat of the script. It is composed of an if statement which begins with "if" and ends with "fi". The first line of the if statement sets a condition that will either be true or false:

if ! (`ps wxu $USER | grep -q [n]etscape`)

The ! is the not operator, which tells if to only activate the then clause if what is in the () is not true. Basically, use the ps command to search for the first instance of Netscape for this user. If there is no process ID dealing with Netscape, continue the script by echoing the text enclosed in quotes:

echo "Clearing Netscape cache..."

and by clearing the Netscape cache in that user's home directory:

rm -rf ~/.netscape/cache/*

However, if there is a process ID dealing with Netscape, leave the if statement instead of executing the then clause.

Save your file; I saved mine as clean. You can call your script whatever you want, just don't give it a name that is already being used by another command. To double-check, try this in another virtual terminal:

whereis -b potential_scriptname

If your answer is a pathname, there already is a command with that name, so try another name. If all you get is an echo like this:

whereis -b clean
clean:

it is probably safe to use that name for your script.

Once you've saved your script, you still need to use the chmod command to make it executable:

chmod +x clean

It is also a good idea to create a bin directory in your home directory to store your scripts:

cd
mkdir bin
mv clean ~/bin

Finally, you should test that your script actually works before telling cron to use it. If you are in the bin directory, type:

./clean

If you're in any other directory, type:

clean

If you are in the C shell and receive a "Command not found" message, type:

rehash

and try again.

If you receive an error message when your script executes, you most likely have a typo somewhere in your file. Look for the typo and try running your script again until it runs without any error messages.

Now we're ready to create a crontab file to tell cron to run our script and our find command. Log in as a regular user; I'm logged in as the user genisis. Now type:

crontab -e

to invoke the crontab utility in editor mode. If you're real quick, you'll see this message before entering vi:

crontab: no crontab for genisis - using an empty one

Since we're in vi, press ESC then i to enter insert mode; then enter the following text. Make sure you do not press the Enter key when you type in the find command; vi will wrap the text for you to fit your screen.

Note that the syntax is slightly different than in the system crontab file as there is no "who" field. Since this crontab will have the same name as the user, the "who" will be the user who made the crontab. When you are finished, double-check for typos, then press ESC and then wq to save your changes and quit. If you input an invalid value in the time fields, crontab will complain and ask if you want to re-enter the editor. Say yes, and look for your typo. Otherwise, you should see this message:

crontab: installing new crontab

Tomorrow when you check your e-mail, you will see two e-mails from cron with the results of your crontab entries. If your commands were successful, they should look like this:

A few final notes on crontabs: if you ever want to view your crontab, you can type:

crontab -l

If you want to make changes to your crontab, use crontab -e again.

Only root can see which users have installed crontabs. Become the superuser and try this:

ls /var/cron/tabs
genisis

You should see a new entry with the same name as the user who just created the crontab.

We've just scratched the surface of the functionality of crontabs. In later articles, as we discover new commands and learn to write more scripts, we'll have additional entries to add to our crontab files.

Next week, we'll delve into the fine art of digging for the useful information concealed in manpages.

Dru Lavigne
is a network and systems administrator, IT instructor, author and international speaker. She has over a decade of experience administering and teaching Netware, Microsoft, Cisco, Checkpoint, SCO, Solaris, Linux, and BSD systems. A prolific author, she pens the popular FreeBSD Basics column for O'Reilly and is author of BSD Hacks and The Best of FreeBSD Basics.