Classic Shell Scripting - Arnold Robbins [189]
The output of ps is not guaranteed to be in any particular order, and since the list of processes is continually changing, its output usually differs on each run.
Since the process list is dynamic, many users prefer to see a continually updating ps-like text display, or a graphical representation thereof. Several utilities provide such display, but none is universally available. The most common one is top, now standard in many Unix distributions.[1] We consider it one of those critical utilities, like GNU tar, that we immediately install on any new system that does not have a native version. On most systems, top requires intimate knowledge of kernel data structures, and thus tends to require updates at each operating system upgrade. Also, top (like ps) is one of those few programs that needs to run with special privileges: on some systems, it may be setuid root.
Here's a snapshot of top output on a moderately busy multiprocessor compute server:
$ top
Show top resource consumers
load averages: 5.28, 4.74, 4.59 15:42:00
322 processes: 295 sleeping, 4 running, 12 zombie, 9 stopped, 2 on cpu
CPU states: 0.0% idle, 95.9% user, 4.1% kernel, 0.0% iowait, 0.0% swap
Memory: 2048M real, 88M free, 1916M swap in use, 8090M swap free
PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
2518 jones 1 0 0 506M 505M run 44:43 33.95% Macaulay2
1111 owens 1 0 19 21M 21M run 87:19 24.04% ocDom
23813 smith 1 0 19 184M 184M cpu/0 768:57 20.39% mserver
25389 brown 1 1 19 30M 23M run 184:22 1.07% netscape
...
By default, top shows the most CPU-intensive processes at the top of the list, which is usually what you are interested in. However, it accepts keyboard input to control sort order, limit the display to certain users, and so on: type ? in a top session to see what your version offers.
Other commands useful for listing processes or showing various system loads are shown in Table 13-1.
Table 13-1. Useful system load commands
System
Commands
All
iostat , netstat, nfsstat, sar, uptime, vmstat, w, xcpustate,[2] xload, and xperfmon
Apple Mac OS X
pstat
BSD
pstat and systat
GNU/Linux
procinfo
HP Alpha OSF/1
vmubc
IBM AIX
monitor
SGI IRIX
gr_osview and osview
Sun Solaris
mpstat , perfmeter, proctool, prstat, ptree, and sdtperfmeter
[2] Available at ftp://ftp.cs.toronto.edu/pub/jdd/xcpustate/.
In most cases, the shell waits for a process to terminate before processing the next command. However, processes can be made to run in the background by terminating the command with an ampersand instead of a semicolon or newline: we used that feature in the build-all script in Section 8.2. The wait command can be used to wait for a specified process to complete, or, without an argument, for completion of all background processes.
Although this book mostly ignores interactive features of the shell, we note that bg, fg, jobs, and wait are shell commands for dealing with still-running processes created under the current shell.
Four keyboard characters interrupt foreground processes. These characters are settable with stty command options, usually to Ctrl-C (intr: kill), Ctrl-Y (dsusp: suspend, but delay until input is flushed), Ctrl-Z (susp: suspend), and Ctrl-\ (quit: kill with core dump).
It is instructive to examine a simple implementation of top, shown in Example 13-1. The security issues addressed by the /bin/sh - option, and the explicit setting of IFS (to newline-space-tab) and PATH should be familiar from their treatment in Section 8.1. We require a BSD-style ps because it provides the %CPU column that defines the display order, so PATH must be set to find that version first. The PATH setting here works for all but one of our systems (SGI IRIX, which lacks a BSD-style ps command).
Example 13-1. A simplified version of top
#! /bin/sh -
# Run the ps command continuously, with a short pause after
# each redisplay.
#
# Usage:
# simple-top