Staying aware of what's happening on your machine is a big part of system administration. The two most important things to watch on a day-to-day basis are (1) disk space and (2) load on the processor(s). Run out of disk space, and many processes stop working because they have no place to create temporary data. Let a runaway program consume too much processor time, and everything else slows down.
Anticipating the need to add disk space before you run out is an important system-administration function.
You can view both available and used space for the entire system or for any directory in the file system.
To see a summary of disk usage for the entire system:
If you supply a directory as an argument to df , it shows a listing for only the file system containing the directory, so
df -k .
shows a listing only for the file system containing the current directory (which is also a tricky way of finding out which file system the current directory is located in).
df -k /Users
shows a listing only for the file system containing /Users . If the directory is a symlink (see "About Links [the Unix Version of Aliases]" in Chapter 5), then df reports on the file system containing the directory that the link points to, not the link itself.
To see disk usage for a particular directory:
M EANING /U SE
Follows symbolic links in the command-line arguments. (Symbolic links encountered inside directories are not followed.)
Follows all symbolic links.
Does not follow symbolic links.
Displays something for every file counted.
Displays sizes in kilobytes instead of the system default for measuring files (usually 512-byte "blocks").
Displays a grand total at the end.
Displays a subtotal for each directory on the command line.
Does not follow symbolic links that point to directories on other volumes .
Compare with Aqua: Activity Monitor Application
Mac OS X comes with a nice GUI application called Activity Monitor, located in the /Applications/Utilities folder, which lets you monitor active processes, memory, network activity, and so on. Still, you may want to use the command-line tools for several reasons:
To see disk usage for several directories:
localhost:~ vanilla$ du -sk Documents Pictures Movies 82740 Documents 71204 Pictures 320 Movies localhost:~ vanilla$
Add the -c option to get a grand total of disk space used. For example:
sudo du -skc /Users/*
If you have read Chapter 2, "Using the Command Line," then you already have seen two of the best tools for monitoring running processes: ps and top . (See "About Commands, Processes, and Jobs," in Chapter 2.)
Reading the man pages for ps and top yields a great deal of information about the kinds of things you can monitor on your system. Here are some highlights for the top command, which shows a wealth of information in real time. You might want to compare what we show here with the various views of system activity in the Activity Monitor application, which we mentioned earlier. For example, in Activity Monitor you may limit the display of active processes to ones associated with windows in the Aqua interface ("Windowed Processes").
Figure 11.24 is an annotated example of output from top , showing the most significant indicators of system use.
Note how the ID of each process is displayed. If a process gets out of control, you can use the kill command (described in Chapter 2) to terminate it. You need to use sudo to kill processes you didn't start yourself. Be very careful, since you could crash the machine by killing a system process, such as the process 1 ( launchd ).
If you use top frequently, you will notice that your system spends a great deal of time doing very little. It is quite common for computers, especially desktop machines used by only a few people, to run at 90 percent idle. Most of the computing power in the world is wasted , just generating heat and performing no useful work. For ways to put those spare CPU cycles to work for a good cause, see Seti@home (http://setiathome.ssl.berkeley.edu), distributed.net (www.distributed.net), and Xgrid (www.apple.com/server/macosx/features/ xgrid .html).
Another noteworthy command is uptime , which shows the same thing as the first line of output from the w command. The top command also displays the information from uptime in its first few lines, as well as a great deal of other information.
To see system load and uptime:
vm_statTool for Monitoring Memory Usage
Another command worth knowing about is vm_stat ( virtual memory statistics ), often called vmstat on other Unix systems. vm_stat reports on several aspects of memory usage, in real time.
vm_stat is normally executed as
which gives a summary since startup and then a new line every 5 seconds. It keeps running until you press .
Interpreting the output of vm_stat takes some experience, but the pageout column is similar to the data shown by top that column should be zero most of the time. Otherwise, either your system is short of memory or something is using it up.