How to Use the Command 'ps' (with examples)
- Osx
- December 17, 2024
The ps
command in Unix-like operating systems is a potent tool for monitoring and managing system processes. It provides a snapshot of the current processes, showing details about each one, such as the process ID (PID), the parent process ID (PPID), CPU and memory usage, and much more. This command is essential for system administrators and users who need to understand what processes are running on their system or troubleshoot issues by examining process states.
Use case 1: List all running processes
Code:
ps aux
Motivation: By listing all running processes, users gain insight into what tasks their operating system is executing at any given moment. This information can be crucial for diagnosing high system load or identifying unauthorized applications running on the machine.
Explanation:
aux
: This option provides a comprehensive list of all processes. Thea
option tellsps
to list processes of all users, including those without a terminal. Theu
option presents the output in a user-oriented format. Thex
option includes 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.1 22536 2672 ? Ss 08:34 0:01 /usr/lib/systemd/systemd --system --deserialize 29
daemon 1020 0.0 0.5 94568 11672 ? Ssl 08:34 0:10 /usr/bin/docker-containerd
Use case 2: List all running processes including the full command string
Code:
ps auxww
Motivation: The full command string helps in situations where the start of the command doesn’t provide sufficient context. This is especially useful for long commands where the critical details appear towards the completion of the command string.
Explanation:
auxww
: Besides whataux
does, theww
inauxww
forcesps
to not truncate command lines in the display, making it possible to see long command lines in their entirety.
Example Output:
root 1 0.0 0.1 22536 2672 ? Ss 08:34 0:01 /usr/lib/systemd/systemd --system --deserialize 29
daemon 1020 0.0 0.5 94568 11672 ? Ssl 08:34 0:10 /usr/bin/docker-containerd --config /etc/docker/daemon.json
Use case 3: Search for a process that matches a string
Code:
ps aux | grep string
Motivation: Sometimes systems are running a multitude of processes, making it cumbersome to locate a specific one. This command simplifies this task by allowing users to filter processes containing a specific string, which could be part of the command or any attribute shown in the process list.
Explanation:
ps aux
: Generates a comprehensive list of all processes.| grep string
: Pipes the list intogrep
, which searches for the specified ‘string’. This is particularly useful for isolating processes by name or attribute.
Example Output:
user1 21137 0.0 0.2 436656 9288 ? Sl 09:00 0:00 some-process-name --option
Use case 4: Get the parent PID of a process
Code:
ps -o ppid= -p pid
Motivation: Each process has a parent process that initiated it, which can be critical when tracking process hierarchies or diagnosing process spawning issues, particularly in troubleshooting scenarios where understanding which process spawned another is required.
Explanation:
-o ppid=
: Customizes the output to show only the parent PID (PPID) by itself for clarity.-p pid
: Specifies the process ID (PID) of the process for which you want to find the parent PID. This PID needs to be replaced with the actual PID of interest.
Example Output:
2345
Use case 5: Sort processes by memory usage
Code:
ps -m
Motivation: Sorting processes by memory usage helps quickly identify memory hogs on the system that can degrade performance. This is vital for optimization and ensuring smoother functioning of applications by mitigating excessive memory consumption.
Explanation:
-m
: Directsps
to sort the running processes by memory usage, placing those utilizing the most memory at the top.
Example Output:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1835 user1 20 0 12.732g 1.625g 27468 S 1.0 10.5 1:12.53 application-x
2905 user1 20 0 457564 146344 11248 S 0.3 0.9 0:10.25 process-y
Use case 6: Sort processes by CPU usage
Code:
ps -r
Motivation: When a system experiences high load, sorting processes by CPU usage readily identifies which processes are taxing the CPU. This can be indispensable for maintaining optimal system performance and making informed decisions, such as which processes to terminate or allocate more resources to.
Explanation:
-r
: Arranges the processes list by CPU usage, which allows users to see which processes are consuming the most CPU time at the top of the list.
Example Output:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2905 user1 20 0 457564 146344 11248 S 5.5 0.9 0:10.25 cpu-intensive-process
1835 user1 20 0 12.732g 1.625g 27468 S 1.0 10.5 1:12.53 less-active-process
Conclusion:
The ps
command provides a powerful and versatile set of functionalities for monitoring, analyzing, and managing system processes. Whether you’re a system administrator troubleshooting system issues or an enthusiast exploring how Unix-like systems manage processes, these use cases illustrate how ps
can give you the necessary insight into process management.