oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Learning the Mac OS X Terminal, Part 3
Pages: 1, 2, 3

The Weekly Script

The weekly script performs three important tasks, none of which provides any output to the report except a statement that the command was performed.


One of the most useful Unix CLI utilities is locate, a lightning-fast file finder. locate does its magic by searching through a database of filenames created by indexing every pathname on your system. Instead of scanning your disks to find a file, locate just whips through its pre-indexed database, and returns results almost immediately.

However, locate results are only as accurate as its database. Files added after the database has been built will not be "located." locate is not the tool for every search, but with weekly database rebuilding, itís great for quickly finding that long-lost file you know is tucked away somewhere on your drive. The first task of the weekly script, then, is to rebuild the locate database.

If you are antsy to try locate for yourself, have a look at the short locate tutorial included in the article found here.

The weekly script updates another important database used by the whatis command. whatis is a nifty little memory jogger that quickly shows you the function of a given command, like this:

[localhost:~] chris% whatis netstat
netstat(1)               - show network status

What whatis displays is, in fact, the first line of a commandís "man page". If youíre not already familiar with man pages, you should be. These comprise the massive collection of online Unix documentation included with Mac OS X. Look here for a great tutorial on using man pages.

The weekly script, then, creates a whatis database from all man pages it finds, allowing whatis to return an answer faster than you can say, "Duh!"

Finally, the weekly script also rotates several other log files, including those for ftp and netinfo.

The Monthly Script

Thereís actually very little to the monthly script, but what it does provide can be pretty interesting, if you like to know where all your time goes. The monthly scriptís first task is to run the "connect time accounting" utility ac. ("Connect" here means "logged in.")

When run from the monthly script, ac will report the cumulative time, in hours, each user account has been logged in since the last time the script ran, as well as the total for all users:

Doing login accounting:
	total      714.22
	chris      548.76
	miho       101.77
	andy        54.39
	jonny        9.18
	test1        0.06
	ftp          0.06

ac calculates these totals by reading the current wtmp log file, which logs every login and logout. You can view this list anytime with the last command:

[localhost:/var/log] chris% last 
chris     ttyp2                     Thu Feb 21 16:18   still logged in
chris     ttyp1                     Thu Feb 21 16:16   still logged in
chris     console  localhost        Thu Feb 21 16:02   still logged in
reboot    ~                         Thu Feb 21 16:01 

So how does ac know to restart the accounting each month? Well, right after ac reports its findings, the monthly script rotates the wtmp logs, creating a new empty wtmp file to start logging to. The next time the monthly script runs, then, ac will do its accounting based on this new file.

Now that you have a good idea of what the three cron jobs do, and what to look for in the reports, I want to take a final look back at the procedure we followed and give a couple of "variations on the theme."

Using Other Mail Clients

Of course, not everyone uses as their regular email client, and launching it just to read the cron reports is not very convenient. Itís not an entirely hopeless situation, given these two alternatives:

First, use the command line mail utility. Since by now, you probably always have Terminal open anyway :-), you can just type in mail to read any incoming reports. And as you may have noticed, you are alerted of any new mail to your Unix account whenever you open a new Terminal window. Hereís one tutorial on using mail:

Second, add your regular email address to the ~root/.forward file. If youíre lucky enough to have an email account that this works with, this is your best solution. The reasons it might not work are complicated, but the easiest way to test is to use the CLI mail utility to send yourself an email:

[localhost:~] chris% mail
Subject: Test message
Hey, it worked!
[localhost:~] chris%

If you do get the message, then you can simply add the address to the .forward file, or, if youíll never be checking your Unix mail, replace the one thatís there using pico. Hereís the easy command to get right into it:

[localhost:~] chris% sudo pico ~root/.forward

To add an address, follow the previous one with a comma and a space before entering the next address:


Mail forwarded to your regular account, then, will show up in whatever email client you use. Using Eudora or Entourage, for example, you could also create a rule to route the cron reports into their own folder.

Pages: 1, 2, 3

Next Pagearrow