BSD DevCenter
oreilly.comSafari Books Online.Conferences.


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

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.

Open up your favorite text editor and type in the following:


#First, double-check that the user is not 
#currently using Netscape
#Then remove the contents of all the subdirectories 
#in the Netscape cache

if ! (`ps wxu $USER | grep -q [n]etscape`)
        echo "Clearing Netscape cache..."
        rm -rf ~/.netscape/cache/*


echo "Exiting...."

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:


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

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:

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:


If you're in any other directory, type:


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


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.

#every morning at 4:32 search and 
#destroy all core or dead files
32 4 * * * find / \( -name "*.core" -or -name "dead.*" \) -print
-exec rm -rf {} \;

#run the script that clears the Netscape 
#cache every morning at 2:48
48 2 *  *  *  ~genisis/bin/clean

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:

From Thu Sep 14 04:38:31 2000
Date: Thu, 14 Sep 2000 04:38:50 -0400 (EDT)
From: genisis (Cron Daemon)
To: genisis
Subject: cron <> find / \( -name "*.core" -or -name "dead.*" \) -print -exec rm -rf {} \;

find: /usr/games/hide: Permission denied
find: /var/spool/opielocks: Permission denied
find: /var/cron/tabs: Permission denied
find: /var/games/hackdir: Permission denied
find: /stand/etc: Permission denied
find: /etc/isdn: Permission denied
find: /etc/uucp: Permission denied
find: /root/mail: Permission denied

From Thu Sep 14 02:51:36 2000
Date: Thu, 14 Sep 2000 02:52:01 -0400 (EDT)
From: genisis (Cron Daemon)
To: genisis
Subject: cron <> ~genisis/bin/clean

Clearing Netscape cache...

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

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.

Read more FreeBSD Basics columns.

Discuss this article on Daemon News.

Return to the BSD DevCenter.


Sponsored by: