# Cheatsheet & Examples: ps

The `ps` command reports information about the current processes running on a Unix-like system. It is used to monitor, manage, and debug process activity.

## Display processes for the current terminal session

Example Usage:
`ps`

What it does:
Shows a list of processes associated with the current terminal session, including the process ID (PID), terminal, CPU usage, and command name.

Command-line Arguments Explained:

- None: Defaults to showing processes for the current user and terminal session.

## Display a detailed list of all processes

Example Usage:
`ps aux`

What it does:
Provides a comprehensive list of all running processes, including user, CPU, memory, and command-line details.

Command-line Arguments Explained:

- `a`: Displays processes for all users.
- `u`: Shows user-oriented format with detailed resource usage.
- `x`: Includes processes without a controlling terminal (background processes).

## Display all processes in a full format

Example Usage:
`ps -ef`

What it does:
Lists all processes in a detailed format, including the user, PID, parent PID, CPU usage, and command.

Command-line Arguments Explained:

- `-e`: Selects all processes.
- `-f`: Displays the full format listing (long format).

## Display processes by PID

Example Usage:
`ps -p [PID]`

What it does:
Shows information about a specific process using its process ID (PID).

Command-line Arguments Explained:

- `-p`: Specifies the process ID to monitor.

## Display processes in long format

Example Usage:
`ps -l`

What it does:
Shows process details in a long format, including PID, PPID, command, and resource usage.

Command-line Arguments Explained:

- `-l`: Enables long (detailed) listing format.

## Display processes with custom output fields

Example Usage:
`ps -o [field1],[field2]`

What it does:
Customizes the output to display only specified fields, such as PID, command, or memory usage.

Command-line Arguments Explained:

- `-o`: Defines custom output fields. Replace `[field1],[field2]` with desired fields like `pid,comm,mem`.

## Sort processes by CPU usage

Example Usage:
`ps -eo %cpu,comm --sort -%cpu`

What it does:
Lists processes sorted by CPU usage in descending order, highlighting the most resource-intensive tasks.

Command-line Arguments Explained:

- `-e`: Selects all processes.
- `-o`: Specifies output fields, e.g., `%cpu` (CPU percentage) and `comm` (command name).
- `--sort`: Sorts the output by the specified field ( `-` denotes descending order).

## Display processes owned by a specific user

Example Usage:
`ps -u [username]`

What it does:
Lists processes owned by the specified user, showing their resource usage and command details.

Command-line Arguments Explained:

- `-u`: Displays processes for the given username. If no username is provided, it defaults to the current user.

## Display process tree structure

Example Usage:
`ps --forest`

What it does:
Generates a tree-like view of processes, showing parent-child relationships.

Command-line Arguments Explained:

- `--forest`: Displays processes in a hierarchical tree format, with indentation for child processes.

## Display threads of a specific process

Example Usage:
`ps -T -p [PID]`

What it does:
Lists all threads associated with the specified process, including thread IDs (TID) and related details.

Command-line Arguments Explained:

- `-T`: Shows threads instead of processes.
- `-p`: Specifies the process ID to monitor.

## Display processes with their start time and command line

Example Usage:
`ps -eo pid,etime,cmd`

What it does:
Shows the PID, elapsed time, and full command line for all processes.

Command-line Arguments Explained:

- `-e`: Selects all processes.
- `-o`: Customizes output to include `pid` (process ID), `etime` (elapsed time), and `cmd` (command line).

## Filter processes by command name

Example Usage:
`ps -C [command_name]`

What it does:
Displays processes matching the specified command name, such as `nginx` or `bash`.

Command-line Arguments Explained:

- `-C`: Filters processes by the exact command name provided. Use `--help` to view supported options.
