Search notes:

Shell command: ps

ps shows a snapshot of a set of processes.
The default set of shown processes are those
The processes that are included in the set of shown processes can be configured with the simple and list process selection options.

Option types

A confusing aspect of ps is that some command line options come
For example, -f specifies full-format listing while f prints the processes in an ASCII art process tree.

Shown columns

By default, ps lists the four columns
PID process id
TTY terminal associated with the process
TIME cumulative CPU time
CMD command that started the process
$ ps
    PID TTY          TIME CMD
   5105 pts/3    00:00:00 bash
   5373 pts/3    00:00:00 ps
These four columns can be specified explicitly with
$ ps -o pid,tty,time,cmd
    PID TT           TIME CMD
   5105 pts/3    00:00:00 -bash
   5374 pts/3    00:00:00 ps -o pid,tty,time,cmd
If a BSD style option is used, the output add process state (stat=STAT) and shows command argugments (args=COMMAND) instead of the executable name:
$ ps t pts/3
    PID TTY      STAT   TIME COMMAND
   5105 pts/3    Ss     0:00 -bash
   5397 pts/3    R+     0:00 ps t pts/3
Some command line options allow to modify the columns that are printed.

Options

Simple process selection

a Lift the BSD-style «only yourself» restriction, which is imposed upon the set of all processes when some BSD-style (without "-") options are used or when the ps personality setting is BSD-like. The set of processes selected in this manner is in addition to the set of processes selected by other means. An alternate description is that this option causes ps to list all processes with a terminal (tty), or to list all processes when used together with the x option.
-A, -e All processes.
-a All processes except both session leaders and processes not associated with a terminal.
-d All processes except session leaders.
--deselect, -N Reverse selection
g Obsolete, only useful in sunos4: really all processes, even session leaders.
T All processes associated with this terminal (TTY). Identical to the t option without any argument.
r Restrict the selection to only running processes.
x Lift the BSD-style "must have a tty" restriction, which is imposed upon the set of all processes when some BSD-style (without "-") options are used or when the ps personality setting is BSD-like. The set of processes selected in this manner is in addition to the set of processes selected by other means. An alternate description is that this option causes ps to list all processes owned by you (same EUID as ps), or to list all processes when used together with the a option.

Selecting processes by a list

-C cmdlist Select by command name. Compare with pidof and pgrep
-G grplist, --Group grplist Select by real group ID (RGID) or group name, i.e. the group of the user who created the process.
--group grplist Select by effective group ID (EGID) or name, i.e. the group whose file access permissions are used by the process. See also the -g option.
-g sesorgrplist Select by session (if all elements sesorgrplist are numeric) or by effective group name (if at least one element is textual). Compare with -s and --group options.
p pidlist, -p pidlist, --pid pidlist Select by process ID (PID). p does print the STAT column and process arguments, -p and --pid do not print these. ps p $procid is equal(?) to ps -$procid.
--ppid pidlist Select child processes of given (parent) process IDs.
q pidlist, -q pidlist, --quick-pid pidlist Quick mode: Select by process ID
-s sesslist, --sid sesslist Select by session ID.
t ttylist Select by tty. Nearly identical to -t and --tty, but can also be used with an empty ttylist to indicate the terminal associated with ps. Same as T option.
-t ttylist, --tty ttylist Select by tty which can be specified by /dev/ttyS1, ttyS1 or simply S1. A hyphen (-t - or --tty -) indicates to select processes not attached to any terminal.
U userlist, -u userlist, --user userlist Select by effective user ID (EUID) or user name.
-U userlist, --User userlist Select by real user ID (RUID) or user name, i. e. the user who created the process.

Controlling output

-c Show different scheduler information for the -l option.
--context Display security context format (SELinux).
-f Do full-format listing. This option can be combined with many other UNIX-style options to add additional columns. It also causes the command arguments to be printed. When used with -L, the NLWP (number of threads) and LWP (thread ID) columns will be added. See the c option, the format keywords args and/or comm.
-F Extra full format. See the -f option, which -F implies.
--format fmt, -o fmt, o fmt User-defined format (allows to select columns). Add = to column name to suppress header. See format specifiers (whose list can be displayed with ps L).
j BSD job control format.
-j Jobs format.
l Display BSD long format.
-l Long format. See also -y.
-M, Z Add a column of security data. (for SELinux).
O fmt is preloaded o (overloaded). The BSD O option can act like -O (user-defined output format with some common fields predefined) or can be used to specify sort order. Heuristics are used to determine the behavior of this option. To ensure that the desired behavior is obtained (sorting or formatting), specify the option in some other way (e.g. with -O or --sort). When used as a formatting option, it is identical to -O, with the BSD personality.
-O fmt Like -o, but preloaded with some default columns. Identical to -o pid,format,state,tname,time,command or -o pid,format,tname,time,cmd
-P Add PSR column (i. e. the processor id)
s Display signal format. I believe this to be equivalent to ps -o uid,pid,pending,blocked,ignored,caught,stat,tty8,bsdtime,args
u Display user-oriented format. I believe this to be equivalent to ps -o user,pid,%cpu,%mem,vsz,rss,tty8,stat,start_time,bsdtime,args
v Display virtual memory format. I believe this to be almost equivalent to ps -o pid,tty8,stat,bsdtime,maj_flt,trs,drs,rss,%mem,args
X Register format. I believe this to be equivalent to ps -o pid,stackp,esp,eip,tmout,alarm,stat,tty8,bsdtime,args
-y Do not show flags; show rss in place of addr, can only be used with -l.

User defined format (fmt)

fmt (options --format, -o and o) is given a list of comma or blank-separted format specifiers to specify the columns to be shown.
The PS_FORMAT environment specifies a default format.
Column width will increase as needed for wide headers; this may be used to widen up columns such as WCHAN (ps -o pid,wchan=WIDE- WCHAN-COLUMN -o comm).
Column width can be explicitly controlled:
ps -o pid,wchan:42,cmd
Headers can be renamed:
ps -o pid,ruser=RealUser -o comm=Command
The result of ps -o pid=X,comm=Y is apparently different on various systems, some recognize the comma as column separator, others take it as part of the column header.
Use multiple -o to disambiguate.
If all columns are renamed to the empty string, no header will be shown:
ps -o pid= -o comm=

Output modifiers

c Show the true command name (executable file rather than argv value), args format keyword becomes comm.
--cols n Set screen width.
--columns n Set screen width.
--cumulative Include some dead child process data (as a sum with the parent).
e Show the environment after the command.
f ASCII art process hierarchy (forest).
--forest ASCII art process tree.
h No header. (or, one header per screen in the BSD personality). The h option is problematic. Standard BSD ps uses this option to print a header on each page of output, but older Linux ps uses this option to totally disable the header. This version of ps follows the Linux usage of not printing the header unless the BSD personality has been selected, in which case it prints a header on each page of output. Regardless of the current personality, you can use the long options --headers and --no-headers to enable printing headers each page or disable headers entirely, respectively.
-H Show process hierarchy (forest).
--headers Repeat header lines, one per page of output.
k spec, --sort spec Specify sorting order with a multi-letter format specifier. The syntax is [+|-]key[,[+|-]key[,…]]. Default is order in increasing order (number or lexicographically). For example: ps jax --sort=uid,-ppid,+pid
--lines n Set screen height.
n Force numeric output for wchan and user format specifiers, including all types of UID and GID.
--no-headers Print no header line at all. --no-heading is an alias for this option.
O order Sorting order (overloaded). The BSD O option can act like -O (user-defined output format with some common fields predefined) or can be used to specify sort order. Heuristics are used to determine the behavior of this option. To ensure that the desired behavior is obtained (sorting or formatting), specify the option in some other way (e.g. with -O or --sort).
--rows n Set screen height.
S Sum up some information, such as CPU usage, from dead child processes into their parent. This is useful for examining a system where a parent process repeatedly forks off short-lived children to do work.
w Wide output. Use this option twice for unlimited width.
-w Wide output. Use this option twice for unlimited width.
--width n Set screen width.
The following example uses the output modifiers e to print the process' environment.
-p $$ selects the current process.
-o cmd selects a minimal set of columns.
The output is piped into tr ' ' '\n' so that each environment variable's name and value is printed on a separated line:
$ ps -p $$  -o cmd e --no-headers | tr ' ' '\n'
Debian ~/github/temp/temp ps -p $$  -o cmd  e --no-headers | tr ' ' '\n'
-bash
HOSTTYPE=x86_64
LANG=en_US.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib
TERM=xterm-256color
HOME=/home/rene
USER=rene
LOGNAME=rene
SHELL=/bin/bash
…

Threads

H Show threads as if they were processes.
-L Show threads, possibly with LWP and NLWP columns.
m, -m Show threads after processes. See also the pending format specifier.
-T Show threads, possibly with SPID column.

Misc

--help section section is one of simple, list, output, threads, misc, or all (or short: s, l, o, t, m or a).
--info Print debugging info.
L List all format specifiers. See also --format, -o and o.
V, -v or --version Print the procps-ng version.

Show all processes

The -e option shows every process.
ps -e
Using -A (apparently standing for all) is equivalent.

Long format ps-long

ps -l and ps l show some default columns such as STAT for the process state.
Compare with ps f.

Showing a process with a specific PID

ps --pid $PPID
ps  -q   $PPID

Showing processes of a given user

With the U option (note: no hyphen), the process of the given user id or user name can be queried.
# Showing my processes.
# 
# id -u returns my user id
# 
# U selects processes with the given user id.

ps -o pid,ppid,cmd  U$(id -u)
Github repository shell-commands, path: /ps/U_userid-or-username

Select displayed column (-o)

ps can be invoked with the -o option (user defined format) to specify which columns should be reported.
Show Process ID (pid), Parent Process ID (ppid) and command with all its name (arg)
ps -o pid,ppid,args
The (identical) BSD option of -o is o, the GNU long option is --format.

Change column width

The width of the columns that were selected with -o can be changed with :n:
The width of cmd is specified to be 40, the width of the user column to be 15:
ps -e -o cmd:40,user:15,time
The width of the last column is not truncated even when specifying its width

cmd vs comm, changing the process name

The following C program changes the process name as reported with the column cmd.
//
//  gcc change-command-name.c -o change-command-name
//
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char** argv) {

  printf("go to another terminal and enter\n");
  printf("  ps --pid %d -o comm=Process-Name,cmd\n", getpid());

  printf("press any key\n");
  getc(stdin);

  strncpy(*argv, "Changed process title", strlen(*argv));
  *argv[strlen(*argv)] = '\0';
  printf("I have changed to process title, execute the command again.\n");

  printf("press any key\n");
  getc(stdin);

}
Github repository shell-commands, path: /ps/change-command-name.c
When the program runs, it first asks to print the two columns cmd and comm.
After pressing a key, the program changes its programm name and asks again to print cmd and comm.
It can be seen that the process name was changed.

Some columns

STAT: the process state. An s indicates a session leader, a + that the process is part of a foreground process group. These attributes are related to job control (pressing Ctrl-Z or starting a process in the background &).

Show process tree

With f (which stands for forest), the parent child relationship between the processes can be displayed:
ps f
ps xaf
Compare with pstree.

TODO

ps -s: signal

See also

Linux process states
Shell commands (such as top or pgrep)
/proc/$pid
docker ps

Index