Process Monitoring with ps


Process Monitoring with ps

Rather 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:

  • Output in wide format (ww), which allows the process listing to wrap to multiple lines rather than being cropped to the width of your terminal (a single w would show only the first 132 characters)

  • Usernames along with processes (u)

  • All users' processes, not just your own (a)

  • All processes, regardless of whether they have an associated terminal (x)

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 -ax | grep httpd 40563  ??  S      0:54.73 /usr/local/sbin/httpd 40564  ??  S      0:55.30 /usr/local/sbin/httpd 40565  ??  S      0:56.03 /usr/local/sbin/httpd 40566  ??  S      1:00.16 /usr/local/sbin/httpd 40567  ??  S      1:05.13 /usr/local/sbin/httpd

ps Output Explained

Listing 15.3 shows some sample output from ps -wwaux.

Listing 15.3. Sample Output from ps -waux

USER    PID %CPU %MEM   VSZ   RSS  TT  STAT STARTED      TIME COMMAND root      1  0.0  0.1   724   244  ??  ILs   5:05PM   0:00.09 /sbin/init -- root      2  0.0  0.0     0     8  ??  DL    5:05PM   0:00.61 [g_event] root      3  0.0  0.0     0     8  ??  DL    5:05PM   0:08.17 [g_up] root      4  0.0  0.0     0     8  ??  DL    5:05PM   0:04.96 [g_down] root      5  0.0  0.0     0     8  ??  DL    5:05PM   0:00.00 [thread taskq] root     11 98.8  0.0     0     8  ??  RL    5:05PM  20:27.49 [idle] root     27  0.0  0.0     0     8  ??  WL    5:05PM   0:12.44 [swi4: clock sio]

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 top

It'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.




FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net