How to Use the Command 'ps' (with examples)
The ps
command, short for “process status,” is a powerful utility in Unix-like operating systems that provides detailed information about active processes. It is a fundamental tool in system administration for monitoring and controlling processes. With ps
, users can display currently running processes on a system, view detailed information about these processes, perform process management tasks, and filter or sort process data based on various criteria. This versatility makes ps
an essential tool for managing system resources efficiently.
Use case 1: List all running processes
Code:
ps aux
Motivation:
Listing all running processes is invaluable for system administration and troubleshooting. By running the ps aux
command, administrators can gain a comprehensive view of all processes currently executing on the system, whether launched by users or system daemons. This bird’s-eye view allows operators to identify resource hogs, assess system performance, and determine which processes are necessary or redundant. It’s particularly useful for diagnosing performance issues related to CPU, memory, or I/O load.
Explanation:
ps
: Invokes the process status command.a
: Displays all processes on a terminal, not just those belonging to the user.u
: Displays the process list in a user-oriented format.x
: Shows processes that are not associated with a terminal.
Example output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 15840 1148 ? Ss 09:25 0:00 /sbin/init
root 2 0.0 0.0 0 0 ? S 09:25 0:00 [kthreadd]
user 12124 0.2 1.6 113460 56364 ? Sl 09:30 0:05 /usr/bin/firefox
Use case 2: List all running processes including the full command string
Code:
ps auxww
Motivation:
The default output of the ps aux
command sometimes truncates long command strings. This can obscure important details about how a process was initiated, particularly if commands have numerous arguments or are called with extensive file paths. The auxww
option ensures that the full command is displayed, offering a complete picture necessary for in-depth diagnostics or when verifying the exact parameters a process was started with.
Explanation:
ps
: Begins the process status report.a
: Lists all users’ processes.u
: Provides a user-friendly display.x
: Includes processes without a controlling terminal.ww
: Ensures wide output for the complete command lines.
Example output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 15840 1148 ? Ss 09:25 0:00 /sbin/init splash
user 12124 0.2 1.6 113460 56364 ? Sl 09:30 0:05 /usr/bin/firefox -profile /home/user/.mozilla/firefox
Use case 3: Search for a process that matches a string
Code:
ps aux | grep [s]tring
Motivation:
When managing systems, administrators often need to locate specific processes quickly, especially when dealing with large outputs. Searching for a specific string in the process list using grep
filters the results to show only the processes of interest. However, using the brackets around the first letter of the string ([s]tring
) cleverly avoids the grep
process itself from appearing in the result, a common annoyance when searching processes.
Explanation:
ps aux
: Lists all running processes.|
: Pipes the list to the next command.grep [s]tring
: Searches for processes containing the specific string. The brackets ensure that thegrep
command itself is not included in the output.
Example output:
root 3127 0.3 0.0 12376 924 ? Ss 10:15 0:01 string_process
Use case 4: List all processes of the current user in extra full format
Code:
ps --user $(id -u) -F
Motivation:
A user might want to view detailed information about only their own processes, without cluttering the output with system or other users’ processes. This use case is particularly applicable when a user needs to manage their own workload, optimize resource usage, or debug personal scripts. The extra full format provides comprehensive details, including the full command line, scheduling priority, and associated system resources.
Explanation:
ps
: Initiates the process status command.--user $(id -u)
: Uses the current user’s ID to filter processes, obtained withid -u
.-F
: Selects extra full format for more detailed information.
Example output:
UID PID PPID C STIME TTY TIME CMD
1000 15267 15199 0 11:00 pts/2 00:00:00 /bin/bash
1000 15302 15267 1 11:01 pts/2 00:00:02 python my_script.py
Use case 5: List all processes of the current user as a tree
Code:
ps --user $(id -u) f
Motivation:
Viewing processes in a tree format is beneficial for understanding process hierarchies and dependencies. This layout allows users to easily identify parent-child relationships between processes, which is critical for debugging and process management tasks. For example, nested processes might be indicative of a beyond-normal workload or of misconfigured daemons attempting a restart loop.
Explanation:
ps
: Starts the process status examination.--user $(id -u)
: Filters the current user’s processes using the user’s numeric ID.f
: Presents processes in a tree format to visualize the process hierarchy.
Example output:
PID TTY STAT TIME COMMAND
15267 pts/2 Ss 0:00 /bin/bash
15302 pts/2 Sl 0:01 \_ python my_script.py
Use case 6: Get the parent PID of a process
Code:
ps -o ppid= -p pid
Motivation:
Identifying the parent process ID (PPID) of a process can be crucial in situations where a process’s origin is uncertain, or its behavior seems incorrect. Investigating the PPID helps understand which process started a given task, assisting in tracing process lineage in systems where scripts generate subprocesses. This knowledge is key to resolving issues or optimizing scripts and applications interacting at multiple levels.
Explanation:
ps
: Initiates the process status command.-o ppid=
: Specifies that only the parent process ID should be outputted, with no headers.-p pid
: Targets the specific process ID for which you want to find the parent PID.
Example output:
1214
Use case 7: Sort processes by memory consumption
Code:
ps --sort size
Motivation:
Sorting processes by memory consumption is a critical step in diagnosing and managing memory usage in systems. It allows system administrators and developers to quickly identify memory-intensive processes that could be causing performance bottlenecks or that might need to be offloaded or optimized. This sorting helps in addressing memory leaks, managing quotas, or finding candidates for scaling in cloud environments.
Explanation:
ps
: Calls the process status command.--sort size
: Sorts the output by the size field, typically representing memory usage.
Example output:
PID TTY STAT TIME COMMAND
12124 ? Sl 0:03 /usr/bin/java -jar application.jar
31342 ? S 0:00 /usr/bin/python3 /path/to/script.py
Conclusion:
The ps
command is an indispensable tool for Unix-like operating systems due to its ability to display process information in versatile formats. From listing processes to filtering, sorting, and displaying processes in a hierarchy or by detailed specifics, ps
offers essential insights required for effective system monitoring and management. Understanding these commands enriches one’s ability to maintain system health, optimize performance, and manage resources wisely.