We’re here to help explain how to use ps command on Linux and garner information on the status of different processes run by the system. So read on to know more!
Processes are computer programs in execution. In a multitasking environment like Linux, the operating system runs several processes simultaneously. This is done by switching the execution of processes and assigning them to processors one by one.
Also, there are several attributes maintained by the operating system for the owner’s processes and the associated security attributes.
How to Use ps Command on Linux
In Linux, you can use the ps command to see the details of all the processes running in the system. Information like process name, owner, session, etc. can be obtained about each process. In this guide, we discuss the ps command in detail along with its various options.
ps command
The ps command stands for process status. It displays the list of all the processes running in the system with their attributes. The information about these processes is read from the /proc file system. This is a virtual file system containing virtual files. Among the information displayed by the ps command includes:
PID
– The unique process Id associated with every processTTY
– The terminal user is logged intoTIME
– The amount of time the process has been running in minutesCMD
– The command that has launched the process
Hint: Sometimes, when we run the ps command, it displays the TIME
as 00:00:00
. This means the central processing unit (CPU) has not been allocated by the operating system to this process so far.
For instance, the bash command is generally associated with 0 CPU time because it is a parent process for executing other processes and doesn’t utilize CPU time.
ps command usage with no arguments
When the ps command is run without any arguments, it shows the list of interactive processes in the system. This is the list of processes associated with the shell. The following command illustrates how we can find the list of interactive methods. On your system, open the terminal and type the following command:
$ps
Listing all the processes
Irrespective of the process types, all the processes running in the system can be seen using the –A
option. The following command illustrates this:
$ps -A
Also, you can use the –e
option to view all the processes as follows:
$ps -e
Showing all processes not associated with the terminal
The following command can be used to list down all the processes that are not associated with any terminal:
$ps -a
Showing all the running/active processes
Every processes that are running or active in the background can be shown by typing the following command:
$ps -r
Processes of the current user
All the processes of the current user can be listed down with the help of the –x
option. The following command illustrates it:
$ps -x
Listing down a specific process attribute
The –C
option can be used to list down a specific process by name. We can specify the process name. For instance, the following command list down the details of the gedit
process:
$ps -C gedit
Listing down a specific group of process
We can specify a specific group for the process. The –G
option is used for this purpose, and the group ID is the user group that created the process. The following command shows how we can list the process for the group faculty:
$ps -G faculty
Listing down a process based on their process Id
We can also use the process ID to search for the process and list its attributes. The following command searches for the process with ID 4792
and shows its attributes:
$ps -p 4792
Viewing all the processes belonging to a specific session Id
To view the processes belonging to a specific session ID, -s
or –sId
options can be used with the ps command as shown below:
$ps -s 1391
or
$ps -sId 1391
Select the process based on tty
Based on the terminal (tty
), the processes can be filtered using the –t
or –tty
option. The following command shows that:
$ps -t pts/0
List the processes for a specific user
The –U
command can list the process by name or ID for a specific user. The following command selects the processes for user khan as follows:
$ps -U khan
Listing process as a tree
We can also use the --forest
command to list down the process as a tree. It will display the processes along with their parent-child relationship. The following command illustrates this:
$ps --forest -x
Other options
There are many other options available with the ps command. For instance, we can list down the process with a complete format listing by using the –f
option. You can also use -F
to view the full extra format, --format
to view the process details with the user-defined format.
And the -j
option will be used for BSD
format, l
for long BSD
format, -v
for virtual memory format. For instance, to list down the process with their id, name, CPU, memory usage, and command, the following can be used:
$ps -e -o pid,uname,pcpu,pmem,comm
You can even rename the labels for each column as follows:
$ps -e -o pid=PID,uname=USERNAME,pcpu=CPU_USAGE,pmem=%MEM,comm=COMMAND
You can list down the threads of a process using –T
or –L
options. Moreover, you can also sort the processes while listing them down. For instance, the following process lists down the processes based on their memory:
$ps -eo pid,ppid,cmd,%mem,%cpu --sort=%mem
Tip: You can pip the ps command with grep to custom search for a specific process
We have discussed the ps command that can be used to find out the information about processes running in the system. Moreover, we can filter these processes based on the current user, group, session, terminal, etc. We can also specify the custom format to list the processes. We have discussed a range of options for the ps command.
More information can be obtained about the ps command from the Linux manual.
If this guide helped you, please share it.