ps Command in Linux to View Process Information
Sometimes, seasoned Linux users need to review running processes in order to make changes, review the information of a process, or kill the process. On the other hand, Linux newbies will be in need of the ps command sooner or later.
So it’s better to get familiar with the ps command in Linux, and if you already are, you can skip to the “Basic ” or “Advanced” use cases of the ps command sections in the article regarding your needs.
But by any chance, if you have recently bought a Linux VPS and need more information on the ps command in Linux, we recommend reading the whole article. In this article, we talk about the ps command, the way it works, its use cases, and its various options.
How does the ps command work?
Simply put, the ps command in Linux lists process information. The “ps” phrase is the abbreviation of “Process Status.” The ps command retrieves the data from the /proc directory and translates them for the user.
The /proc directory is a filesystem that records and stores process and kernel information. The options, or flags, used in the ps command make it even more useful for advanced Linux users to filter information based on desired factors.
The Linux ps command’s syntax is as follows:
syntax:
ps [options]
The ps command options can be written in three forms, plus their mixes:
- UNIX: It’s necessary to put a dash (-) before the option in UNIX-formatted flags. Example:
ps -aux
- BSD: There is no need to put a dash before the option in BSD-formatted flags. Example:
ps aux
- GNU-formatted flags need two dashes and are used for long options. Example:
ps --ppid 1
Now, let’s dive into the world of the ps command’s use cases in Linux.
The ps command use cases in Linux
Generally, the ps command lists and displays all the running processes in the Linux kernel. So, we can say that its main functionality is to display process information, including the process’s unique ID (PID), the terminal or user associated with the process, and the command launching the process.
But its functionality is not limited to this list. We can gather other important information using the Linux ps command, too. Information like CPU usage time by the process, elapsed time, the starting date of the process, and so on.
Additionally, we will cover all types of columns in the ps command’s result further in this article. Thus, we split the use cases of the ps command with examples into the “Basic” and “Advanced” sections so you can review them smoothly.
- Basic Usage
This section will present the bare ps command, its basic options with examples, the result’s four basic columns, and what they mean. Note that we can use the ps command with or without options in Linux.
We start with the bare ps command in Linux with example:
1. Listing the processes related to the current shell
Using the bare ps command lists currently running processes in the shell.
ps
As you witness, we have four columns: PID, TTY, TIME, and CMD. You may wonder what these four abbreviations stand for.
- PID: PID is the short form of “Process ID”. Every running process has a specific ID to differentiate it from other processes.
- TTY: TTY stands for “Terminal Type”, demonstrating the terminal used to run the specified process.
- TIME: Time is about the time the process has utilized the CPU, not the amount of time the process has been running.
- CMD: This element shows the command used in launching the process.
2. Using Linux ps command to view all running processes
As mentioned, we can use the ps command with or without options. From now on, we will discuss the many types of options and arguments that can be added to the basic ps command.
Let’s start with viewing all running processes in ubuntu and other distributions. To do so, put “-A” or “-e” at the end of the command.
ps -A
ps -e
Note that the command is unrelated to any terminal session whenever TTY is a question mark.
3. Viewing terminal-related processes
If you want to view the processes that are associated with the terminal, simply run this command:
ps -T
- By running this command, we will no longer see a question mark in the TTY column.
- This command has two functionalities: one to issue terminal-related processes and the other to display the threads of a specific process (discussed further in the article.)
4. Display the processes not related to the terminal
To view all processes except session leaders and processes related to the terminal, run the Ubuntu ps command:
ps -a
A session leader is a process that starts other processes, e.g., bash. As you can see, unlike the ps command itself, the “ps -a” doesn’t show the bash process.
5. Listing all running processes with the command’s full name
To list all currently running processes with the command’s full name, type the command below, then hit the Enter button:
ps -ax
- Advanced Usage
From now on, we will discuss advanced options of the ps command in Linux ubuntu, CentOS and etc. So, before reading this part, ensure you have at least intermediate-level coding knowledge.
1. List all processes in BSD format
In case you want to list processes in BSD format, these ps commands can help:
ps -au
or
ps au
and
ps aux
In the picture above,
- %CPU: The CPU utilization of the process is shown in this column.
- %MEM: Shows the percentage of the process’s resident size to the physical memory on the machine.
- VSZ: VSZ shows the amount of virtual memory the process is consuming in KiB.
- RSS: This is the size of the physical memory that the process is using.
- STAT: This column is about the status of the process generated in codes.
- START: The START column shows the time or the date when the process has begun.
The 13 codes of the STAT column are as follows:
Process State Codes | Meaning |
---|---|
D | uninterruptible sleep |
I | Idle kernel thread |
R | running, runnable or on run queue |
S | interruptible sleep (waiting for an event to complete) |
T | stopped by job control signal |
t | stopped by debugger during the tracing |
Z | defunct "zombie" process, terminated but not reaped by its parent |
< | high-priority |
N | low-priority |
L | has pages locked into memory |
s | is a session leader |
1 | is multi-threaded |
+ | is in the foreground process group |
2. Full format listing with Linux ps command
To perform a full format listing, run either of these commands:
ps -ef
ps -eF
In the ps command format of listing, the meaning of columns is as follows:
- UID shows the user running the process.
- PPID is the ID of the parent process.
- C is the CPU utilization of the process.
- STIME is equal to the START, displaying the time or date when the process took place.
- SZ is about virtual memory usage.
- PSR demonstrates the processor to which the process is currently assigned.
3. To filter processes in regard to user
We can use the -u option after the ps command ubuntu to filter processes according to the user.
ps -u username
For example:
ps -u root
4. To view the thread of a specified process
In order to filter the ps command’s results based on the threads of a particular process, all you need to do is to add one of the -L or -T options to the ps command followed by the PID:
ps -L PID
ps -T PID
Examples:
ps -L 77
ps -T 529
The LWP and SPID columns refer to the thread ID.
5. Showing all the processes run by the root user
In case you want to view the list of all the processes run by the root user, execute:
ps -U root -u root
6. Using the ps command in Linux to show group processes
To list all the processes run under a specific group, execute the ps command as below:
ps -fG group_name
or
ps -fG groupID
For instance :
ps -fG root
7. ps command in Linux to search for process PID
It’s a common thing not to know the PID of a process. To search for the PID of a process, run this command:
ps -C process_name
For example:
ps -C bash
As you can see, we have three bash processes running on this kernel, each with a unique PID. But the bare ps command in Linux only shows the one related to the current terminal ( PID: 220430.)
But the ps command with the -C option followed by the process name engulfs all processes, showing all three (bash processes) in this example ( PIDs: 219199 / 219658 / 220430.)
We can say the ps command with the -C option is an all-encompassing command to search for PID.
8. Using the PID to search for the process name
On the other hand, sometimes we know the PID but not the process name. Having the PID in hand, we can search for the process name by the ps command. Note that we can only search for the running processes whose names are listed below the CMD column.
ps -p PID -o comm=
For example:
ps -p 84 -o comm=
In the command above, “-p” option indicates PID, “-o” option refers to the output format, and “comm=” displays the command’s name column.
To check whether the result is correct, let’s search the PID by the command’s name:
ps -C kswapd0
As you can see, the PID of the “kswapd0” process is 84.
9. Listing processes in accordance with PID
Processes can be listed according to their PID with the ps command ubuntu:
ps -fp PID
For instance:
ps -fp 88
10. Display process hierarchy in a tree diagram
Generally, most of the processes are child processes of parent ones. Being aware of this relationship between processes can be useful. The Linux ps command below lists the processes named “bash.”
ps -f --forest -C bash
11. Displaying the child processes of a parent one
We can list all the child processes of a parent process. If you want to list all the child processes of the “systemd” process, run the following:
ps -o pid,uname,comm -C systemd
The first process (PID: 1) is the main systemd2 process, and the other (PID: 217424) is its child process. We also can list all the child processes of a child process with the PID.
To list the child processes of a child process, run:
ps --ppid PID no.
In systemd2 case, we should run:
ps --ppid 1
As seen in the photo, the child process of systemd2 (PID: 217424) is again listed by the later command (marked by a tiny star.)
12. List processes with specific columns
We can set the ps command in Linux to display results with the desired columns. For instance:
ps -e -o pid,uname
This command will list the processes with only PID and USER columns:
The -o option defines the output format.
13. Renaming columns in the ps command search result
The ps command in Linux can be used to rename columns. To do so, you can make your specific rename command according to the command below:
ps -e -o pid=Algeria,uname=America
In this example, first, we limited listing columns to PID and uname, then renamed them to “Algeria” and “America,” respectively. You can rename any column to any phrase this way.
14. ps command to display the elapsed time of every process
In order to know how long a process has been running on the system, run:
ps -e -o pid,comm,etime
“etime” phrase in the command refers to the Elapsed column.
15. Search for a specific process using the ps command with grep
You can use the grep command in Linux with the ps command if you need to search for a specific process. For instance:
ps -ef | grep systemd
Conclusion
The ps command in Linux is a built-in utility to display the status of processes. In addition, its flags and options enable you to dig further into the details of any given process.
In this article, we analyzed the various options of the ps command in Ubuntu Linux, its syntax, and the types of the result columns. Also, we provided examples with each option.
At first, we talked about the basics of the ps command and then described how to filter results based on PID, USER, child/parent relationships, or threads. This article even covers renaming any column in the ps command’s results.