Process Monitoring with psRather than being an interactive, real-time monitoring program like top, ps works a lot more like ls (hence the name). It's an instantaneous listing of all the processes that are running at the time you execute it. It provides all the information that top does as well as extra details about many of the values. By default, if you run ps without any arguments, you will get a listing of only the processes owned by you that are attached to terminals (that is, those that have been run from a login session). A large array of command-line options can give you more widereaching results, and each can be found documented in man ps. You specify these options as follows: # ps wwaux This combination of options produces the following:
In short, this combination of options displays a list of every process on the system, with as much detail as possible. You can filter the output in certain ways using the built-in options. For instance, eliminate the x option to show only the processes attached to users' terminal sessions, and drop the a option to show only your processes (or use -U to specify another userfor example, -U frank). Many other options of this type can be found in the man page for ps. Beyond the built-in filters, though, you will need to use ps in conjunction with grep (as you saw in Chapter 12, "The FreeBSD Filesystem") to filter based on the process name. An example is shown in Listing 15.2. Listing 15.2. Sample ps Output, Filtered Through grep
ps Output ExplainedListing 15.3 shows some sample output from ps -wwaux. Listing 15.3. Sample Output from ps -waux
Processes in the output from ps are sorted by PID rather than any of the reported metrics (unless you used one of the special sorting options described in the man page). You do get most of the same information that top gives you, albeit in a more cryptic format and with a few slight variations. When to Use ps Instead of topIt's generally more useful to use top to gather general metrics on your processes; but ps is intended rather for looking up PIDs of specific processes and seeing the complete command line for each one. It's quicker than top, and scriptable. For example, you can write scripts that extract the PID for a process from the output of ps and send signals to that PID, all through the use of shell commands and pipes. This is particularly helpful when you need to rein in a runaway process or change the priority on a task. These are functions that we will now review in detail. |