## 24.5. The ps Command

The ps command varies from system to system. (The ps on one Red Hat Linux system reads a PS_PERSONALITY environment variable with 21 possible settings!) This article describes several different versions. Yours is probably different in some ways, so check your ps manual page for details.

The ps command produces a report summarizing execution statistics for current processes. The bare ps command lists the process ID, the terminal from which the command was started, how much CPU time it has used, and the command itself. The output looks something like this (it differs by system):

>       PID TT STAT  TIME COMMAND
>      1803 p5 IW    0:00 -csh (csh)
>      1883 p5 IW    0:04 vi outline
>      1811 p6 IW    0:01 -csh (csh)
>      5353 p6 TW    0:01 vi 4890

By default, ps lists only your own processes. There are many times, though, when it's desirable to have a more complete listing with a lot of data about all of the processes currently running on the system. The options required to do this differ between BSD Unix and System V. Under BSD Unix, the command is ps -aux, which produces a table of all processes, arranged in order of decreasing CPU usage at the moment when the ps command was executed. [The -a option gives processes belonging to all users, -u gives a more detailed listing, and -x includes processes that no longer have a controlling terminal (Section 24.6). -- TOR] It is often useful to pipe this output to head (Section 12.12), which will display the most active processes:

>     % ps -aux | head -5
>     USER       PID %CPU %MEM   SZ  RSS TTY STAT  TIME COMMAND
>     martin   12923 74.2 22.5  223  376 p5  R     2:12 f77 -o foo foo.F
>     chavez   16725 10.9 50.8 1146 1826 p6  R N  56:04 g94 HgO.dat
>     ng       17026  3.5  1.2  354  240 co  I     0:19 vi benzene.txt
>     gull      7997  0.2  0.3  142   46 p3  S     0:04 csh

The meanings of the fields in this output (as well as others displayed by the -l option to ps) are given in Table 24-1.

The first line of this output shows that user martin is running a FORTRAN compilation (`f77`). This process has PID (Section 24.3) 12923 and is currently either running or runnable. User chavez's process (PID 16725), executing the program g94, is also running or runnable, though at a lowered priority. From this display, it's obvious who is using most system resources at this instant: martin and chavez have about 85% of the CPU and 73% of the memory between them. However, although it does display total CPU time, ps does not average the `%CPU` or `%MEM` values over time in any way.

#### Table 24-1. ps command output fields

Column[72] | Contents
--- | ---
USER (BSD) | Username of process owner
UID (System V) | User ID (Section 24.3) of process owner
PID | Process ID
%CPU | Estimated fraction of CPU consumed (BSD)
%MEM | Estimated fraction of system memory consumed (BSD)
SZ | Virtual memory used in K (BSD) or pages (System V)
RSS | Real memory used (in same units as SZ)
TT, TTY | Terminal port associated with process
STAT (BSD), S (System V) | Current process state; one (or more under BSD) of:
| R: Running or runnable
| S: Sleeping
| I: Idle (BSD); intermediate state (System V)
| T: Stopped (Section 23.1)
| Z: Zombie process (Section 24.19)
| D (BSD): Disk wait
| P (BSD): Page wait
| X (System V): Growing,waiting for memory
| K (AIX): Available kernel process
| W (BSD): Swapped out
| N (BSD): Niced (Section 26.5, Section 26.7), execution priority lowered
| > (BSD): Execution priority artificially raised (Section 26.7)
TIME | Total CPU time used
COMMAND | Command line being executed (may be truncated)
STIME (System V) | Time or date process started
C (System V), CP (BSD) | Short term CPU-use factor; used by scheduler for computing execution priority (PRI below)
F | Flags associated with process (see ps manual page)
PPID | Parent's PID
PRI | Actual execution priority (recomputed dynamically)
NI | Process nice number (Section 26.5)
WCHAN | Event process is waiting for

[72] Some vendors add other fields, such as the processor number for multiprocessors and additional or different process states (as in the AIX K field). These codes may differ from vendor to vendor: for example, the 0 code under Stardent Unix means a process that is actually running (and R means runnable), while 0 under AIX means a nonexistent process.

A vaguely similar listing is produced by the System V ps -ef command:

>     $ ps -ef
>        UID   PID  PPID   C    STIME     TTY  TIME CMD
>       root     0     0   0 09:36:35       ?  0:00 sched
>       root     1     0   0 09:36:35       ?  0:02 /etc/init
>       ...
>       gull  7997     1  10 09:49:32   ttyp3  0:04 csh
>     martin 12923 11324   9 10:19:49   ttyp5 56:12 f77 -o foo foo.F
>     chavez 16725 16652  15 17:02:43   ttyp6 10:04 g94 HgO.dat
>         ng 17026 17012  14 17:23:12 console  0:19 vi benzene.txt

The columns hold the username, process ID, parent's PID (the PID of the process that created it), the current scheduler value, the time the process started, its associated terminal, its accumulated CPU time, and the command it is running. Note that the ordering is by PID, not resource usage.

AIX's version of the ps command supports both BSD and System V options. The BSD options are not preceded by a hyphen (which is a legal syntax variation), and the System V options are. Thus, under AIX, `ps -au` is not the same as `ps au`. The command is the System V version, however, even if its output is displayed with the BSD column headings. Thus, ps aux output is displayed in `PID` rather than `%CPU` order.

ps is also useful in pipes; a common use is:

>     % ps -aux | grep chavez

to see what user chavez has currently running. Under System V, use `ps -u chavez`.

Another way to view the process information is with the top command. Unlike ps, top is an interactive screen program that updates its information every few seconds. It's a good way to get a quick pulse of your system. Not only is process information displayed, but memory statistics and the system uptime are also shown. You can find the full range of available interactive commands by typing `h` once top has started. You can sort processes in a variety of ways including CPU and memory usage, as well as by user. You can even kill processes from within top.

--AF, from Essential System Administration (O'Reilly, 2002), and JJ

Copyright © 2003 O'Reilly & Associates. All rights reserved.