Introduction
The ps
command is one of the most useful tools for monitoring processes running on a Linux system. With ps
, you can get a detailed overview of the active processes, including information such as the process ID (PID), the owning user, CPU and memory usage, and more. This tutorial will walk you through using ps
to monitor and manage processes on your Linux system.
What is the ps
command?
The ps
command is short for "process status". It is a system command that provides a static representation of currently running processes. Unlike other tools such as top
or htop
, ps
does not provide an interactive interface, but returns a snapshot of the processes at the time it is executed.
Installing ps
The ps
command is included by default in almost all Linux distributions, so you should not need to install it. However, if for some reason it is not available, you can install it using your distribution's package manager.
On Debian/Ubuntu:
sudo apt-get install procps
On CentOS/Fedora:
sudo yum install procps
Basic usage of ps
To get a list of all active processes on the system, you can use the ps
command without any options:
ps
This command will return a list of processes associated with the user who executed the command. The list will be limited and will not provide much information.
Output example:
PID TTY TIME CMD
1234 pts/0 00:00:00 bash
5678 pts/0 00:00:00 ps
Common ps
options
To get more detailed information about processes, you can use several options with the ps
command. Some of the more common options are listed below.
ps aux
The aux
option is one of the most used. It shows all the processes in the system, regardless of the session they belong to, and includes detailed information such as CPU and memory usage.
ps aux
Output example:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 19352 1540 ? Ss 09:00 0:00 /sbin/init
root 2 0.0 0.0 0 0 ? S 09:00 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 09:00 0:00 [rcu_gp]
ps -ef
The -ef
option is another common option that displays all processes in a long format. It includes information such as the parent process ID (PPID) and the process startup time.
ps -ef
Output example:
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 09:00 ? 00:00:00 /sbin/init
root 2 0 0 09:00 ? 00:00:00 [kthreadd]
root 3 2 0 09:00 ? 00:00:00 [rcu_gp]
Filtering processes with grep
You can combine ps
with the grep
command to filter processes based on a certain string. For example, to find all processes associated with a specific program, such as firefox
, you can use:
ps aux | grep firefox
Output example:
user 1234 0.5 2.3 253428 47232 ? Sl 09:00 0:01 /usr/lib/firefox/firefox
user 5678 0.0 0.0 14228 1024 pts/0 S+ 09:05 0:00 grep --color=auto firefox
Sort processes by CPU or memory usage
You can also sort processes by CPU or memory usage using the ps
command combined with sort
. For example, to sort processes by CPU usage in descending order:
ps aux --sort=-%cpu
Output example:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 1234 5.2 2.3 253428 47232 ? Sl 09:00 0:05 /usr/lib/firefox/firefox
user 5678 0.5 1.2 123456 24568 ? S 09:00 0:02 /usr/bin/some_program
View processes of a specific user
If you want to display only the processes of a specific user, you can use the -u
option followed by the user name. For example, to display the processes of the user john
:
ps -u john
Output example:
PID TTY TIME CMD
1234 pts/0 00:00:00 bash
5678 pts/0 00:00:00 firefox
Terminate a process
If you have identified a process that you want to kill, you can do so using the kill
command. To kill a process, you need to know its PID. For example, to kill a process with PID 1234
:
kill 1234
If the process does not stop with a simple kill
, you can force it to terminate using the -9
signal:
kill -9 1234
Conclusion
The ps
command is an essential tool for monitoring and managing processes on a Linux system. With the options and techniques described in this tutorial, you should be able to get a detailed overview of active processes, filter processes based on specific criteria, and manage processes effectively. Experiment with different options and combinations to tailor ps
to your specific needs.