Linux ps Command


Introduction

The ps command is a widely used utility in Linux that provides a snapshot of current processes and their status. It helps monitor running processes, identify process ID (PID), terminal type (TTY), CPU time usage, command name, user ID and other information. This article provides a comprehensive overview of the various use cases of the ps command in real life.

Syntax of ps Command

The basic syntax of the ps command is as follows −

$ ps [OPTIONS]

The ps command supports three different syntax styles: Unix, BSD, and GNU. Unix-style syntax can be wrapped and preceded by a hyphen, BSD can be wrapped but not preceded by a hyphen, and GNU syntax uses long options and is preceded by double hyphens.

ps Command Options

The following are some of the switches used with the ps command −

  • ps -ef or ps -aux − List currently running processes in full format

  • ps -ax − List currently running processes

  • ps -u <username> − List processes for a specific user

  • ps -C <command> − List processes for a given command

  • ps -p <PID> − List processes with a given PID

  • ps -ppid <PPID> − List processes with a given parent process ID (PPID)

  • pstree − Show processes in a hierarchy

  • ps -L − List all threads for a particular process

  • ps --sort pmem − Find memory leaks

  • ps -eo − Show security information

  • ps -U root -u root u − Show processes running by root

Use Cases of ps Command

Let’s take a look at real life use cases of ps command which are helpful for system administrators.

List all Running Processes

To view all processes currently running on a system, use the following command −

$ ps -ef
UID    	PID  PPID  C  STIME  TTY     TIME     CMD
root      1 	0    0  Jan31   ?    00:00:00  /sbin/init splash
root      2 	0    0  Jan31   ?    00:00:00  [kthreadd]
root      3 	2    0  Jan31   ?    00:00:00  [rcu_gp]
root      4 	2    0  Jan31   ?    00:00:00  [rcu_par_gp]
root      5 	2    0  Jan31   ?    00:00:00  [kworker/0:0-events]
...

In the above output, PID represents the process ID of the running command, TTY is the type of terminal where the current command is being executed, TIME is the time it takes for the CPU to execute the process, and CMD is the current command.

Lists all Running Processes Filtered by CPU or Memory Usage

To view all currently running processes filtered by CPU or memory usage, use the following command −

$ pd-aux
USER   	 PID   %CPU  %MEM   VSZ   RSS   TTY  STAT  START   TIME  COMMAND
root      1    0.0   0.0   19404  3116   ?    Ss    Jan31   0:00  /sbin/init splash
root      2    0.0   0.0     0 	   0     ?    S	   Jan31   0:00  [kthreadd]
root      3    0.0   0.0     0 	   0     ?    R	   Jan31   0:00  [rcu_gp]
root      4    0.0   0.0     0 	   0     ?    S	   Jan31   0:00  [rcu_par_gp]
root      5    0.0   0.0     0 	   0     ?    S	   Jan31   0:00  [kworker/0:0-events]
...

In the above output, %CPU shows the current CPU usage by the process, %MEM shows the memory usage by the process, VSZ is the size of the virtual memory, and RSS is the size of the resident pool.

List Processes for a Specific User

To view all processes running for a specific user, use the following command −

$ ps -u <username>

For example, to view all processes for the root user, use the following command −

$ ps -u root
USER   	PID  %CPU   %MEM    VSZ   RSS   TTY    STAT  START   TIME  COMMAND
root     1    0.0   0.0    19404  3116   ?    	Ss   Jan31   0:00  /sbin/init splash
root     2    0.0   0.0      0 	    0    ?    	S    Jan31   0:00  [kthreadd]
root     3    0.0   0.0      0 	    0    ?    	R    Jan31   0:00  [rcu_gp]
root     4    0.0   0.0      0 	    0    ?    	S    Jan31   0:00  [rcu_par_gp]
root     5    0.0   0.0      0 	    0    ?    	S    Jan31   0:00  [kworker/0:0-events]
...

List of Processes for a Specific Command

To view all processes running for a specific command, use the following command −

$ ps -C <command>

For example, to view all running processes for the python command, use the following command −

$ ps -C python
USER   	PID  %CPU  %MEM	  VSZ   RSS   TTY   STAT   START   TIME  COMMAND
root   	123   0.0   1.2  95768  23844  ?     S	   12:00   0:05  python
user1  	456   0.2   0.9  95768  23844  ?     S	   13:00   0:03  python
...

List Processes with a Given PID

To view the process with a given PID, use the following command −

$ ps -p <PID>

For example, to view the process with PID 123, use the following command −

$ ps -p 123
USER   	PID  %CPU  %MEM	  VSZ    RSS  TTY   STAT   START   TIME  COMMAND
root   	123   0.0   1.2  95768  23844  ?     S	   12:00   0:05  python

Conclusion

The ps command is an incredibly powerful tool for managing processes on Linux. Whether you want to monitor a specific process, find a process that is taking up too much memory or CPU, or monitor all processes running on your system, the ps command provides the information you need. It is an essential tool for any Linux administrator or power user.

Updated on: 19-Apr-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements