Linux Commands – ps

Linux Commands – ps

Linux Commands – ps

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed chgrp command in Linux which is used to change the group ownership of a file or directory.

https://cloudaffaire.com/linux-commands-chgrp/

In this blog post, we will discuss ps command in Linux. ps command is used to display current processes. ps command prints a snapshot of the currently active processes. If you want to have the real-time view of processes, use the top command.

Linux Commands – ps:

Ps command can be used to print a snapshot of the current processes. Ps command provides lots of options to control the output. You can choose how much data (rows and columns) to display and the format of the display. ps command accepts multiple types of options.

  • UNIX options, which may be grouped and must be preceded by a dash.
  • BSD options, which may be grouped and must not be used with a dash.
  • GNU long options, which are preceded by two dashes.

Note: Options of different types may be freely mixed, but conflicts can appear.

General Options:

OPTIONS DESCRIPTION
-A, -e all processes
-a all with tty, except session leaders
a all with tty, including other users
-d all except session leaders
-N, –deselect negate selection
r only running processes
T all processes on this terminal
x processes without controlling ttys

 

You can use ps -A or -e options to list all processes. By default, ps lists current active processes.

You can use ps -a option to select all processes except both session leaders and processes not associated with a terminal.

You can use ps -d option to select all processes except session leaders. A session leader is a process whose session id and process id are same.

You can use ps –deselect or -N options to select all processes except those that fulfill the specified conditions (negates the selection). There are lots of options for selection which are covered later in this blog.

You can use ps T or t options to select all processes associated with this terminal.

You can use ps r option to returns only running processes (STAT=R). STAT column meaning has been given at the end of this blog.

You can use ps a option to list all processes with a terminal (tty), or to list all processes when used together with the x option. This option lifts the BSD-style “only yourself” restriction.

Selection Options:

OPTIONS DESCRIPTION
-C <command> command name
-G, –Group <GID> real group id or name
-g, –group <group> session or effective group name
-p, p, –pid <PID> process id
–ppid <PID> parent process id
-q, q, –quick-pid <PID> process id (quick mode)
-s, –sid <session> session id
-t, t, –tty <tty> terminal
-u, U, –user <UID> effective user id or name
-U, –User <UID> real user id or name

 

ps command provides selection options to filter the output based on selection criteria. These options accept a single argument in the form of a blank-separated or comma-separated list. For example: ps -p “1 2” -p 3,4.

You can use ps -C cmdlist option to select processes by command name. This selects the processes whose executable name is given in cmdlist.

You can use ps -G grplist or or –Group grplist options to select the processes whose real group name or ID is in the grplist list.

You can use ps -g grplist or –group grplist options to select the processes whose session or effective group name is in the grplist list.

You can use ps p pidlist or -p pidlist or –pid pidlist options to select the processes whose process ID numbers appear in pidlist.

You can use ps –ppid pidlist option to select the processes with a parent process ID in pidlist. That is, it selects processes that are children of those listed in pidlist.

You can use ps q pidlist or -q pidlist or –quick-pid pidlist options to select the processes whose process ID numbers appear in pidlist. With this option ps reads the necessary info only for the pids listed in the pidlist and doesn’t apply additional filtering rules. The order of pids is unsorted and preserved. No additional selection options, sorting and forest type listings are allowed in this mode.

You can use ps -s sesslist or –sid sesslist options to select the processes with a session ID specified in sesslist.

You can use ps t ttylist or -t ttylist or –tty ttylist options to select the processes associated with the terminals given in ttylist. Terminals (ttys, or screens for text output) can be specified in several forms: /dev/ttyS1, ttyS1, S1. A plain “-” may be used to select processes not attached to any terminal.

You can use ps U userlist or -u userlist or –user userlist options to select the processes whose effective user name or ID is in userlist. The effective user ID describes the user whose file access permissions are used by the process.

You can use ps -U userlist or –User userlist options to select the processes whose real user name or ID is in userlist. The real user ID identifies the user who created the process.

Format Options:

ps command supports different format options to control which columns will be displayed in the output. By default, ps command only displays process id, terminal name, time, and command column. But you can use this format option to display different columns as provided in the output table at the end of this blog. Below are the format options available with ps command.

OPTIONS DESCRIPTION
-F extra full
-f full-format, including command lines
f, –forest ascii art process tree
-H show process hierarchy
-j jobs format
j BSD job control format
-l long format
l BSD long format
-M, Z add security data (for SELinux)
-O <format> preloaded with default columns
O <format> as -O, with BSD personality
-o, o, –format <format> user-defined format
s signal format
u user-oriented format
v virtual memory format
X register format
-y do not show flags, show rss vs. addr (used with -l)

 

You can use ps o format or -o format or –format format options to print the output in user-defined format. format is a single argument in the form of a blank-separated or comma-separated list, which offers a way to specify individual output columns. You can also format the Headers with -o option (ps -o pid, ruser=RealUser -o comm=Command) as desired. The output columns and their meaning is given at the end of this blog post.

You can use ps command with different predefined format options to control the column selection in the output.

Output Modifier Options:

You can use ps command output modifier options to change the format of the output. The below table lists the different output modifier options available to ps command.

OPTIONS DESCRIPTION
–context display security context (for SELinux)
–headers repeat header lines, one per page
–no-headers do not print header at all
–cols, –columns, –width <num> set screen width
–rows, –lines <num> set screen height
H as if they were processes
-L possibly with LWP and NLWP columns
-m, m after processes
-T possibly with SPID column
-c show scheduling class with -l option
c show true command name
e show the environment after command
k, –sort specify sort order as: [+|-]key[,[+|-]key[,…]]
L show format specifiers
n display numeric uid and wchan
S, –cumulative include some dead child process data
-y do not show flags, show rss (only with -l)
-V, V, –version display version information and exit
-w, w unlimited output width

 

You can use ps c option to show the true command name rather than command with argv value. Command arguments and any modifications to them are thus not shown.

You can use ps e option to show the environment after the command.

You can use ps –headers option to repeat header lines, one per page of output.

You can use ps –cols n or –columns n or –width n options to set the screen width.

You can use ps –lines n or –rows n options to set the screen height.

You can use ps f or –forest options to print process hierarchy (forest) in ASCII art. This option is similar to pstree command. The -H option does the same thing without ASCII art.

You can use ps h or –no-headers or –no-heading options to suppress printing of headers.

You can use ps w or -w options to set wide output. Use these options twice for unlimited width.

You can use ps S option to 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.

You can use ps –sort spec or k spec to sort the output based on spec. Sorting syntax is [+|-]key[,[+|-]key[,…]]. The “+” sign sorts in ascending order which is the default order and “-” sing is used to sort in descending order.

You can use ps H or -L or m or -m or -T options to display process threads information. Below are the meaning of each thread options.

  • H: Show threads as if they were processes.
  • -L: Show threads, possibly with LWP and NLWP columns.
  • m: Show threads after processes.
  • -m: Show threads after processes.
  • -T: Show threads, possibly with SPID column.

You can use ps L option to list all format specifiers (available output columns in ps command). The below table give you the complete list of columns that can be outputted using ps command.

Format Specifier:

CODE HEADER DESCRIPTION
%cpu %CPU cpu utilization of the process (cputime/realtime ratio) in %
%mem %MEM ratio of the process’s resident set size to the physical memory in %
args COMMAND command with all its arguments as a string.
blocked BLOCKED mask of the blocked signals.
bsdstart START time the command started.
bsdtime TIME accumulated cpu time, user + system.
c C processor utilization.
caught CAUGHT mask of the caught signals
cgname CGNAME display name of control groups to which the process belongs.
cgroup CGROUP display control groups to which the process belongs.
class CLS scheduling class of the process.
cls CLS scheduling class of the process.
cmd CMD see args. (alias args, command).
comm COMMAND command name (only the executable name).
command COMMAND See args. (alias args, command).
cp CP per-mill (tenths of a percent) CPU usage.
cputime TIME cumulative CPU time, “[DD-]hh:mm:ss” format. (alias time).
cputimes TIME cumulative CPU time in seconds (alias times).
drs DRS the amount of physical memory devoted to other than executable code.
egid EGID effective group ID number of the process. (alias gid).
egroup EGROUP effective group ID text of the process. (alias group).
eip EIP instruction pointer.
esp ESP stack pointer.
etime ELAPSED elapsed time since the process was started, in the form [[DD-]hh:]mm:ss.
etimes ELAPSED elapsed time since the process was started, in seconds.
euid EUID effective user ID (alias uid).
euser EUSER effective user name in text. (alias uname, user).
f F flags associated with the process. (alias flag, flags).
fgid FGID filesystem access group ID. (alias fsgid).
fgroup FGROUP filesystem access group ID in text.
flag F see f. (alias f, flags).
flags F see f. (alias f, flag).
fname COMMAND first 8 bytes of the base name of the process’s executable file.
fuid FUID filesystem access user ID. (alias fsuid).
fuser FUSER filesystem access user ID in text.
gid GID see egid. (alias egid).
group GROUP see egroup. (alias egroup).
ignored IGNORED mask of the ignored signals. (alias sig_ignore, sigignore).
ipcns IPCNS Unique inode number describing the namespace the process belongs to.
label LABEL security label, most commonly used for SELinux context data.
lstart STARTED time the command started. See also bsdstart, start, start_time, and stime.
lsession SESSION displays the login session identifier of a process.
luid LUID displays Login ID associated with a process.
lwp LWP light weight process (thread) ID of the dispatchable entity (alias spid, tid).
lxc LXC The name of the lxc container within which a task is running.
machine MACHINE displays the machine name for processes assigned to VM or container.
maj_flt MAJFLT The number of major page faults that have occurred with this process.
min_flt MINFLT The number of minor page faults that have occurred with this process.
mntns MNTNS Unique inode number describing the namespace the process belongs to.
netns NETNS Unique inode number describing the namespace the process belongs to.
ni NI nice value.
nice NI see ni.(alias ni).
nlwp NLWP number of lwps (threads) in the process. (alias thcount).
numa NUMA The node assocated with the most recently used processor.
nwchan WCHAN address of the kernel function where the process is sleeping.
ouid OWNER displays the Unix user identifier of the owner of the session of a process.
pcpu %CPU see %cpu. (alias %cpu).
pending PENDING mask of the pending signals.
pgid PGID process group ID or, equivalently, the process ID of the process group leader. (alias pgrp).
pgrp PGRP see pgid. (alias pgid).
pid PID a number representing the process ID (alias tgid).
pidns PIDNS Unique inode number describing the namespace the process belongs to.
pmem %MEM see %mem. (alias %mem).
policy POL scheduling class of the process. (alias class, cls).
ppid PPID parent process ID.
pri PRI priority of the process. Higher number means lower priority.
psr PSR processor that process is currently assigned to.
rgid RGID real group ID.
rgroup RGROUP real group name.
rss RSS resident set size, the non-swapped physical memory that a task has used.
rssize RSS see rss. (alias rss, rsz).
rsz RSZ see rss. (alias rss, rssize).
rtprio RTPRIO realtime priority.
ruid RUID real user ID.
ruser RUSER real user ID.
s S minimal state display (one character).
sched SCH scheduling policy of the process.
seat SEAT displays the identifier associated with all hardware devices assigned to a specific workplace.
sess SESS session ID or, equivalently, the process ID of the session leader. (alias session, sid).
sgi_p P processor that the process is currently executing on.
sgid SGID saved group ID. (alias svgid).
sgroup SGROUP saved group name.
sid SID see sess. (alias sess, session).
sig PENDING see pending. (alias pending, sig_pend).
sigcatch CAUGHT see caught. (alias caught, sig_catch).
sigignore IGNORED see ignored. (alias ignored, sig_ignore).
sigmask BLOCKED see blocked. (alias blocked, sig_block).
size SIZE approximate amount of swap space that would be required to dirty all writable pages and then be swapped out.
slice SLICE displays the slice unit which a process belongs to.
spid SPID see lwp. (alias lwp, tid).
stackp STACKP address of the bottom (start) of stack for the process.
start STARTED time the command started.
start_time START starting time or date of the process.
stat STAT multi-character process state.
state S see s. (alias s).
stime STIME see start_time. (alias start_time).
suid SUID saved user ID. (alias svuid).
supgid SUPGID group ids of supplementary groups, if any.
supgrp SUPGRP group names of supplementary groups, if any.
suser SUSER saved user name.
svgid SVGID see sgid. (alias sgid).
svuid SVUID see suid. (alias suid).
sz SZ size in physical pages of the core image of the process.
tgid TGID a number representing the thread group to which a task belongs (alias pid). I
thcount THCNT see nlwp. (alias nlwp). number of kernel threads owned by the process.
tid TID the unique number representing a dispatchable entity (alias lwp, spid).
time TIME cumulative CPU time, “[DD-]HH:MM:SS” format. (alias cputime).
times TIME cumulative CPU time in seconds (alias cputimes).
tname TTY controlling tty (terminal). (alias tt, tty).
tpgid TPGID ID of the foreground process group on the tty (terminal) that the process is connected to.
trs TRS text resident set size, the amount of physical memory devoted to executable code.
tt TT controlling tty (terminal). (alias tname, tty).
tty TT controlling tty (terminal). (alias tname, tt).
ucmd CMD see comm. (alias comm, ucomm).
ucomm COMMAND see comm. (alias comm, ucmd).
uid UID see euid. (alias euid).
uname USER see euser. (alias euser, user).
unit UNIT displays unit which a process belongs to.
user USER see euser. (alias euser, uname).
userns USERNS Unique inode number describing the namespace the process belongs to.
utsns UTSNS Unique inode number describing the namespace the process belongs to.
uunit UUNIT displays user unit which a process belongs to.
vsize VSZ see vsz. (alias vsz).
vsz VSZ virtual memory size of the process in KiB (1024-byte units).
wchan WCHAN name of the kernel function in which the process is sleeping.

 

Hope you have enjoyed this article. In the next blog post, we will discuss du command in Linux.

 

Leave a Reply

Close Menu