ps Command in Linux



The ps command in Linux displays information about currently running processes. It provides details such as process IDs (PIDs), CPU and memory usage, and execution time. This information helps with administrative tasks like setting process priorities.

The ps command supports three types of options: Unix-style (preceded by a dash), BSD-style (without a dash), and GNU long options (preceded by two dashes).

Table of Contents

Here is a comprehensive guide to the options available with the ps command −

Syntax of ps Command

The syntax of the ps command in Linux is as follows −

ps [options…]

In the above syntax, the [options…] field is used to specify options to change the command's output.

ps Command Options

The options for the Linux ps command are listed below −

FlagOptionDescription
-A, -eSelects all processes except session leaders.
-aSelects all except session leaders and processes without a terminal.
-dSelects all except session leaders.
--deselectSelects all except those matching specified conditions.
TSelects all processes associated with the current terminal.
rRestricts selection to running processes.
xLifts BSD-style restrictions, listing all user-owned or all processes with -a.
--helpPrints a help message. The section argument can be one of simple, list, output, threads, misc, or all, and can be abbreviated to one of the underlined letters (s, l, o, t, m, a).
--infoPrints debugging information.
LLists all format specifiers.
V, -V--versionPrints the procps-ng version.

Options for process selection by list are listed below −

FlagOptionsDescription
-C cmdlistSelect by command name.
-G grplist--Group grplistSelect by real group ID (RGID) or name.
-g grplist--group grplistSelect by session or effective group name.
p pidlist, -p pidlist--pid pidlistSelect by process ID.
--ppid pidlistSelect by parent process ID.
q pidlist, -q pidlist--quick-pid pidlistSelect by process ID (quick mode).
-s sesslist--sid sesslistSelect by session ID.
t ttylist, -t ttylist--tty ttylistSelect by terminal.
-u userlist--user userlistSelect by effective user ID (EUID) or name.
-U userlist--User userlistSelect by real user ID (RUID) or name.

Options to change the output format are given below −

FlagOptionDescription
-cShow different scheduler information for the -l option.
--contextDisplay security context format (for SELinux).
-fDo full-format listing, adding additional columns and command arguments.
-FExtra full format; implies -f.
o format, -o format--format formatUser-defined format for specifying output columns.
j, -jDisplay job control format.
l, -lDisplay long format (BSD or standard).
M, ZAdd a column of security data (SELinux).
O format, -O formatUser-defined output format with predefined common fields.
-PAdd a column showing processor (psr).
sDisplay signal format.
uDisplay user-oriented format.
vDisplay virtual memory format.
XDisplay register format.
-yShow RSS in place of address, used with -l.
cShow the true command name based on the executable file.
--cols n, --columns n, --width nSet screen width.
--cumulativeInclude some dead child process data with the parent.
-D format--date-format formatSet the date format of the lstart field.
EShow the environment after the command.
f--forestASCII art process hierarchy.
HNo header (or one per screen in BSD mode).
-HShow process hierarchy.
--headersRepeat header lines on each page.
k spec--sort specSpecify sorting order.
--lines n--rows nSet screen height.
nNumeric output for WCHAN and USER.
--no-headersPrint no header line.
O orderSorting order (overloaded).
--signamesShow signal masks using abbreviated signal names.
w, -wWide output; use twice for unlimited width.

To display the process threads, use the options listed below −

OptionDescription
HDisplays threads as if they were separate processes.
-LShows threads, possibly including LWP (Lightweight Process) and NLWP (Number of Lightweight Processes) columns.
m, -mDisplays threads after processes.
-TDisplays threads, possibly including the SPID (Thread ID) column.

Examples of ps Command in Linux

This section discusses how to use the ps command in Linux to display the process details with examples −

  • Displaying All Processes
  • Displaying All Processes in BSD Format
  • Displaying All Processes with Additional Details
  • Displaying All Processes by Username
  • Displaying Processes in Tree Format
  • Displaying a Process with a Specific PID
  • Displaying a Process by the Command Name
  • Displaying Processes with User-defined Columns
  • Displaying Threads
  • Renaming Column Names
  • Displaying Output without Headers
  • Displaying Usage Help

Displaying All Processes

To display processes running in the current terminal, use the ps command without any option −

ps
ps Command in Linux1

To display all the processes currently running in the system, use the -A or -e option with the ps command −

ps -A
ps Command in Linux2

Or −

ps -e

In the output, a ? in the TTY column means the process is not linked to any terminal. This applies to system processes or background tasks that do not interact with a terminal.

Displaying All Processes in BSD Format

To display all processes in BSD format, use the ps command in the following way −

ps au
ps Command in Linux3

Or, use the following command for additional information −

ps aux
ps Command in Linux4

Displaying All Processes with Additional Details

To display all the processes currently running in the system with additional details, use the -f option. It shows a full-format listing, including columns like PPID, STIME, TIME, CMD, and UID.

ps -ef
ps Command in Linux5

Displaying All Processes by Username

To display all processes of a specific user, use the -u or --user option −

ps -u sam

Displaying Processes in Tree Format

To display all processes in the tree format, use the f or --forest option −

ps --forest

For all processes −

ps -e --forest
ps Command in Linux6

Displaying a Process with a Specific PID

To display a process with a specific PID, use the -p or --pid option −

ps -p 2552
ps Command in Linux7

Displaying a Process by the Command Name

To display a process by the command name, use the -C option −

ps -C vim
ps Command in Linux8

Displaying Processes with User-defined Columns

To display all the processes with the user-defined columns, use the o, -o, or --format option with the ps command −

ps -eo pid,comm,%cpu,%mem
ps Command in Linux9

In the above command -e is used to select all processes.

Displaying Threads

To display threads as processes, use the H option −

ps H

To display threads after the process in the output, use the m or -m option −

ps -m

The above command displays the process and thread of the process running in the current terminal session.

Renaming Column Names

To rename the column names of the output, use the following command −

ps -e -o pid=PID,uname=USERNAME,pcpu=CPU,pmem=%MEM,comm=COMMAND
ps Command in Linux10

Displaying Output without Headers

To display output without headers, use the --no-headers option −

ps --no-headers
ps Command in Linux11

Displaying Usage Help

To display usage help for the ps command, use the --help option −

ps --help

To display the debugging information, use the --info option −

ps --info

Conclusion

The ps command in Linux is a powerful tool for displaying information about running processes, such as process IDs, CPU and memory usage, and execution time. It offers various options for filtering, sorting, and formatting the output to meet specific needs. By using different flags and arguments, processes can be displayed in various formats, including by user, command name, or process ID.

Moreover, the ps command can show threads, rename output columns, and display detailed process information, making it an essential tool for system administrators and users monitoring system activity.