LinuxCommandLibrary

ps

List running processes

TLDR

List all running processes

$ ps aux
copy

List all running processes including the full command string
$ ps auxww
copy

Search for a process that matches a string (the brackets will prevent grep from matching itself)
$ ps aux | grep [[s]tring]
copy

List all processes of the current user in extra full format
$ ps [[-u|--user]] $(id [[-u|--user]]) -F
copy

List all processes of the current user as a tree
$ ps [[-u|--user]] $(id [[-u|--user]]) f
copy

Get the parent PID of a process
$ ps [[-o|--format]] ppid= [[-p|--pid]] [pid]
copy

Sort processes by memory consumption
$ ps --sort size
copy

SYNOPSIS

`ps [options]`

Common invocation styles include:
  `ps aux` (BSD style, showing all users' processes, with and without a TTY)
  `ps -ef` (System V style, showing every process in full format)
  `ps -U user -f` (GNU/System V style, processes by user, full format)

PARAMETERS

a
    (BSD style) Display processes for all users, excluding session leaders and processes not associated with a terminal.

x
    (BSD style) Include processes without a controlling terminal in the listing. Often used with 'a' as `ax`.

u
    (BSD style) User-oriented output format. Displays columns like USER, PID, %CPU, %MEM, TTY, STAT, START, TIME, COMMAND.

-e
    (System V/GNU style) Select every process currently running on the system. Similar to `-A`.

-f
    (System V/GNU style) Display a 'full' format listing, providing more details such as UID, PPID, C, STIME, and the full command line. Often used with `-e` as `-ef`.

l
    (System V style) Provide a 'long' format listing with extensive process details including F, S, PRI, NI, ADDR, SZ, and WCHAN.

-o format
    (GNU/System V style) Specify a user-defined output format. Allows selecting specific columns to display (e.g., `ps -eo pid,cmd,%cpu`).

-p pidlist
    (GNU/System V style) Select processes by a comma-separated list of specific Process IDs (PIDs).

-U userlist
    (GNU/System V style) Select processes whose real user ID or username is in the given comma-separated list.

--forest
    (GNU style) Display processes in a tree structure using ASCII art, illustrating parent-child relationships.

-C command
    (GNU style) Select processes by the exact command name provided.

T
    (BSD style) Select all processes associated with the current terminal.

DESCRIPTION

The ps (process status) command is a fundamental Linux utility used to display information about currently running processes. Unlike interactive tools such as `top` or `htop`, `ps` provides a static snapshot of processes at the moment the command is executed, making it ideal for quick checks, scripting, and identifying specific processes by PID, user, or command name.

It can show various details including process ID (PID), parent process ID (PPID), CPU and memory usage, process state, start time, and the command line that launched the process. `ps` supports multiple output formats, including traditional BSD-style, System V-style, and GNU long options, allowing users to customize the displayed columns to their specific needs. Understanding `ps` is crucial for system administration, debugging, and general system monitoring.

CAVEATS

The `ps` command provides a snapshot of processes at the exact moment it is run; it is not a real-time, dynamic display. For continuous monitoring, commands like `top` or `htop` are more suitable.

A significant caveat is the existence of three main option styles: BSD-style (no dash, e.g., `ps aux`), System V-style (single dash, e.g., `ps -ef`), and GNU long options (double dash, e.g., `ps --forest`). These styles often have overlapping functionality but cannot always be reliably mixed, especially BSD and System V options, which can lead to unexpected behavior.

PROCESS STATES (STAT COLUMN)

The `STAT` column in `ps` output indicates the current state of a process. Common state codes include:

  • R: Running or runnable (on run queue)
  • S: Interruptible sleep (waiting for an event to complete)
  • D: Uninterruptible sleep (usually I/O)
  • Z: Zombie process (terminated but parent hasn't reaped it)
  • T: Stopped (by job control or tracing)
  • <: High-priority (not nice to other users)
  • N: Low-priority (nice to other users)
  • s: Is a session leader
  • l: Is multi-threaded
  • +: Is in the foreground process group

COMMON USAGE EXAMPLES

Below are some frequently used `ps` commands:

  • To find a process by name: `ps aux | grep process_name`
  • To see all processes owned by a specific user: `ps -U username -f`
  • To display a process tree: `ps aux --forest`
  • To view processes running on the current terminal: `ps T`
  • To list all processes with PID, parent PID, and command: `ps -eo pid,ppid,cmd`

HISTORY

The `ps` command has deep roots, originating from AT&T Unix and subsequently adopted by BSD Unix. This dual heritage led to the coexistence of two distinct, often conflicting, option syntaxes: the System V-style (using a leading dash for options) and the BSD-style (using options without a leading dash). When Linux emerged, it adopted `ps`, and the `procps` package, which provides the modern `ps` implementation, aimed to integrate these styles and introduce GNU-specific extensions (such as long options and ` --forest`). This evolution resulted in its highly versatile, yet sometimes complex and confusing, command-line interface. Despite these stylistic differences, its core function of providing an immediate view of running processes has remained constant throughout its development.

SEE ALSO

top(1): Display Linux processes, htop(1): Interactive process viewer, pgrep(1): Look up processes based on name or other attributes, pkill(1): Send signals to processes based on name or other attributes, kill(1): Send a signal to a process, pstree(1): Display a tree of processes, nice(1): Run a command with an adjusted scheduling priority, renice(1): Alter the priority of running processes

Copied to clipboard