Introduction
In this tutorial, you'll discover the power of the Linux ps
(process status) command for effective system monitoring and process management. A vital tool for any systemadmin, ps
offers insights into running processes, displaying details like process ID, user, CPU utilization, memory footprint, and more. We begin with the fundamental ps
command usage, then progress to filtering processes by user, and conclude with monitoring CPU and memory consumption of active processes. Mastering these skills is essential for maintaining and optimizing Linux-based systems.
Understand the ps Command
This section introduces the ps
(process status) command, a core Linux utility for displaying information about currently running processes on your system.
The ps
command delivers a snapshot of active processes, including critical data such as the Process ID (PID), associated user, CPU and memory usage, alongside other relevant details.
Let's start by executing the basic ps
command:
ps
Example output:
PID TTY TIME CMD
1234 pts/0 00:00:00 bash
5678 pts/0 00:00:00 ps
The output reveals the current shell process (bash
) and the ps
command instance itself.
To gain access to more in-depth information regarding running processes, consider using the following options:
ps -ef
Example output:
UID PID PPID C STIME TTY TIME CMD
labex 1234 4321 0 10:30 pts/0 00:00:00 /bin/bash
labex 5678 1234 0 10:30 pts/0 00:00:00 ps -ef
The -e
option displays every running process, while -f
provides a comprehensive listing, including the User ID (UID), Process ID (PID), Parent Process ID (PPID), CPU usage (C), Start Time (STIME), Terminal (TTY), CPU Time (TIME), and the executed command (CMD).
Filtering based on specific criteria, such as the process owner, is also possible:
ps -u labex
Example output:
PID TTY TIME CMD
1234 pts/0 00:00:00 bash
5678 pts/0 00:00:00 ps
This command shows all processes owned by the labex
user.
Filter Processes by User
Here, we will explore how to filter the list of running processes based on the user account that initiated them.
The ps
command includes options to filter the process listing, notably the -u
or --user
option. This enables you to specifically view processes owned by a particular user.
Let's begin by listing all processes initiated by the labex
user:
ps -u labex
Example output:
PID TTY TIME CMD
1234 pts/0 00:00:00 bash
5678 pts/0 00:00:00 ps
This command displays all processes owned by the specified labex
user.
You can also refine your search by combining ps
with the grep
command. For instance, to identify all bash
processes owned by labex
:
ps -ef | grep -i labex | grep -i bash
Example output:
labex 1234 4321 0 10:30 pts/0 00:00:00 /bin/bash
In this example, ps -ef
lists all processes, the first grep -i labex
filters for processes owned by labex
, and the second grep -i bash
narrows the results to only bash
processes. The -i
option in grep
performs a case-insensitive search, matching both "labex" and "bash" regardless of capitalization.
Monitor CPU and Memory Usage
This section details how to use the ps
command to monitor CPU and memory resource utilization by running processes.
To observe CPU and memory usage for all processes, the ps
command can be used with the -o
option to customize the output:
ps -eo pid,user,%cpu,%mem,cmd
Example output:
PID USER %CPU %MEM COMMAND
1234 labex 2.0 0.1 /bin/bash
5678 labex 0.5 0.2 ps -eo pid,user,%cpu,%mem,cmd
This command shows the following data for each process:
PID
: The unique Process ID.USER
: The username of the process owner.%CPU
: The percentage of CPU time the process is consuming.%MEM
: The percentage of system memory the process is utilizing.COMMAND
: The command used to launch the process.
Sorting the output by CPU or memory usage allows you to pinpoint the processes with the highest resource demands:
ps -eo pid,user,%cpu,%mem,cmd --sort=-%cpu
Example output:
PID USER %CPU %MEM COMMAND
5678 labex 2.0 0.2 ps -eo pid,user,%cpu,%mem,cmd --sort=-%cpu
1234 labex 1.5 0.1 /bin/bash
This command sorts the output in descending order of CPU usage, placing the most CPU-intensive processes at the top.
Similarly, to sort by memory usage:
ps -eo pid,user,%cpu,%mem,cmd --sort=-%mem
Example output:
PID USER %CPU %MEM COMMAND
5678 labex 2.0 0.2 ps -eo pid,user,%cpu,%mem,cmd --sort=-%mem
1234 labex 1.5 0.1 /bin/bash
This command sorts the output by descending memory usage, highlighting the processes consuming the most memory.
Summary
This tutorial covered the ps
(process status) command, a fundamental Linux tool for examining running processes on a system. Starting with the basic ps
command, you learned how to use options like -e
and -f
to reveal more detailed information. The tutorial also demonstrated how to filter the process list by owner using the -u
or --user
option. Finally, you explored methods for monitoring the CPU and memory utilization of running processes. This knowledge is invaluable for any systemadmin, particularly when managing Linux servers or troubleshooting performance issues. Understanding the ps
command is critical for effective process management and overall system health.