How to use the command 'ps' (with examples)

How to use the command 'ps' (with examples)

The ‘ps’ command provides information about running processes on a system. It can be used to list all processes, search for a specific process, get detailed information about processes, and sort processes based on different criteria.

Use case 1: List all running processes

Code:

ps aux

Motivation: This command is useful when you want to get an overview of all the processes running on your system.

Explanation:

  • ‘ps’ is the command itself.
  • ‘aux’ are options passed to the command.
    • ‘a’ stands for “all” and it displays all processes on the system, not just the ones associated with the current user.
    • ‘u’ stands for “user-oriented” output format, which provides detailed information about each process.

Example Output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3 383636  6192 ?        Ss   May19   0:09 /sbin/init
root         2  0.0  0.0      0     0 ?        S    May19   0:00 [kthreadd]
root         3  4.5  0.0      0     0 ?        S    May19  28:03 [ksoftirqd/0]
...

Use case 2: List all running processes including the full command string

Code:

ps auxww

Motivation: This command is useful when you want to view the full command string of each process, which can provide more detailed information.

Explanation:

  • ‘ps’ is the command itself.
  • ‘auxww’ are options passed to the command.
    • ‘ww’ breaks output lines if they are too long to fit on one line in the terminal.

Example Output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3 383636  6192 ?        Ss   May19   0:09 /sbin/init splash
root         2  0.0  0.0      0     0 ?        S    May19   0:00 [kthreadd]
root         3  4.5  0.0      0     0 ?        S    May19  28:03 [ksoftirqd/0]
...

Use case 3: Search for a process that matches a string

Code:

ps aux | grep string

Motivation: This command is useful when you want to search for a specific process that matches a given string.

Explanation:

  • ‘ps’ is the command that lists all running processes.
  • ‘aux’ are options passed to the ‘ps’ command, as explained in Use Case 1.
  • ‘|’ is a pipe symbol that redirects the output of the ‘ps’ command to the input of the ‘grep’ command.
  • ‘grep’ is a command used for searching patterns within the output.
  • ‘string’ is the string you are searching for in the output.

Example Output: (assuming ‘string’ is “bash”)

username  3155  0.0  0.0  14224   972 pts/0    Ss   09:12   0:00 bash

Use case 4: List all processes of the current user in extra full format

Code:

ps --user $(id -u) -F

Motivation: This command is useful when you want to see detailed information about all processes belonging to the current user.

Explanation:

  • ‘ps’ is the command itself.
  • ‘–user’ option is used to filter and display only processes of a specific user.
  • ‘$(id -u)’ is a command substitution that retrieves the current user’s ID and passes it as an argument to the ‘–user’ option.
  • ‘-F’ is an option that displays extra full format output.

Example Output:

UID   PID  PPID  C    SZ RSS PSR STIME TTY   STAT TIME COMMAND
1000  8342  6557  0 3012508 57840  2 09:12 pts/0 Sl+   0:00 gedit
1000  8453  3274  0 6096232 87568  1 09:13 ?      Sl    0:02 /usr/lib/gnome-settings-daemon/gsd-color
...

Use case 5: List all processes of the current user as a tree

Code:

ps --user $(id -u) f

Motivation: This command is useful when you want to visualize the hierarchy of processes belonging to the current user.

Explanation:

  • ‘ps’ is the command itself.
  • ‘–user’ option is used to filter and display only processes of a specific user.
  • ‘$(id -u)’ is a command substitution that retrieves the current user’s ID and passes it as an argument to the ‘–user’ option.
  • ‘f’ is an option that displays the process hierarchy in a tree format.

Example Output:

  PID TTY      STAT   TIME COMMAND
 3274 ?        Ss     0:00 /usr/lib/gnome-session/gnome-session-binary --session=ubuntu
 3275 ?        S      0:00  \_ /usr/bin/gnome-shell
 3279 ?        S      0:00      \_ /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
...

Use case 6: Get the parent PID of a process

Code:

ps -o ppid= -p pid

Motivation: This command is useful when you want to find the parent process ID of a specific process.

Explanation:

  • ‘ps’ is the command itself.
  • ‘-o ppid=’ is an option that specifies the output format to only include the parent process ID. The ‘=’ removes the column header.
  • ‘-p pid’ is an option that specifies the process ID whose parent process ID we want to find.

Example Output: (assuming ‘pid’ is 12345)

6789

Use case 7: Sort processes by memory consumption

Code:

ps --sort size

Motivation: This command is useful when you want to sort the processes based on the amount of memory they consume.

Explanation:

  • ‘ps’ is the command itself.
  • ‘–sort size’ is an option used to sort the processes by their memory consumption.

Example Output:

PID TTY      STAT   TIME COMMAND
3274 ?        Ss     0:00 /usr/lib/gnome-session/gnome-session-binary --session=ubuntu
8480 pts/0    S      0:00  \_ gnome-terminal-server
...

Conclusion:

The ‘ps’ command is a powerful tool for retrieving information about running processes on a system. It provides various options to customize the output, filter processes, search for specific processes, and sort processes based on different criteria. These use cases demonstrate some of the most common ways to use the ‘ps’ command to gain insights into the processes running on a system.

Related Posts

How to use the command "gh run" (with examples)

How to use the command "gh run" (with examples)

The command “gh run” is a GitHub CLI command that allows users to view, run, and watch recent GitHub Actions workflow runs.

Read More
Proxmox Virtual Environment: qm rollback (with examples)

Proxmox Virtual Environment: qm rollback (with examples)

Use Case 1: Rollback the state of a specific VM to a specified snapshot.

Read More
How to use the command 'cut' (with examples)

How to use the command 'cut' (with examples)

The ‘cut’ command is a versatile utility that allows users to extract specific fields or characters from a file or standard input.

Read More