How to Use the ps Command to Monitor Linux Processes
There will be a time when you need to learn more about a specific process frequently to get its ID so you can parse the “kill” command. Of course, it is not necessary to be for that. This manual will cover the ‘ps’ and ‘top’ commands each must be present in a typical Linux installation.
What is ps?
An application called “ps,” which stands for “process state,” may read all of the process data on your computer, display the results on the terminal, and then shut down. The running status, user and group it belongs to, the process ID, and whether it was started from a terminal are all examples of information that can be provided.
It can also list only a few or all of the processes. It takes both standard style parameters (with a dash, such as “-e”) and BSD style arguments (without a dash), as well as GNU style syntax (those preceded by two dashes). To learn more, refer to the man page.
This Linux command is easy to use, like many others. It prints out some process information about “ps” and the shell that “ps” was running from when it was used without any parameters. Here is a summary of some of the applications for PS.
Listing Process in the Shell
Linux’s most basic version of the ps command displays a list of the active processes in the current shell. The example below demonstrates this.
Listing All Processes
You can use ps to see a list of all the currently executing processes on your computer. The ‘-e’ argument prints a list of all processes and their details.
Couple it with the -‘f’ parameter to get further details about a process, such as its parent process ID (PID) and the username it belongs to. The ‘-F’ argument can be used to get even additional details.
There are some choices to employ if you only want to read details about a certain process, processes belonging to specific users, or filter by process names. For instance, you would issue the following command to list all processes associated with the root user:
ps -u root $ root
Using the ‘-p’ argument, you can also view details about a particular process. To do this, you must first determine the process ID using ‘ps’ or even ‘top.
ps -lp process-id
The ‘-l’ parameter is similar to the ‘-f’ parameter in that it displays more information, but there are a few changes. For example, the user ID (UID) is displayed rather than the username.
Threads can also be shown alongside processes using the ‘L’ argument, which displays the LWP column. IDs for each thread connected to a process are displayed here. The first thread’s ID and the process it originated from are always the same.
If you want to display threads from a specific process, use the ‘-p PID’ argument:
ps -Lfp PID
If you are looking for specific information that you might not be able to access using the options easily, you can pipe output from ps to grep. This technique can be used to locate specific processes with a particular name.
ps -ef | grep chromium-browser
Choosing What To Show
You can provide your output format by using the ‘-o’ parameter. For instance, you can decide whether to display the PID, PPID, user, command, and arguments in the output.
ps -eo pid,ppid,%cpu,%mem,uname,comm,args
Displaying output in BSD format
Run the command to list the processes in BSD format.
Let’s examine the command in detail:
Ps is told to display all processes from all users when the -a flag is used. This does not include the procedures connected to a particular terminal, though.
The -u parameter implies the user-oriented format. It offers more thorough data related to the active processes.
The x flag lists both background processes and processes that are typically launched when a system boots.
Showing output in a user-defined format
You have control over which columns are shown in a user-defined format output. Which columns are shown when the ps command is executed depends on the -o flag. Only the PID and START columns are visible in the example below. Remember that the column options supplied to the ps command are lowercase.
ps -efo pid,start
Show the currently running processes of a user
Use the -u flag as demonstrated to find out which processes are connected to a specific user when you need to in some situations.
ps -u user or $ ps -u uid
Run the following command, for instance, to view processes connected to the user “xitoring”:
ps -u xitoring
A different option is to utilize the user’s uid (User ID). Run the following command to obtain the user’s ID:
Run the command for the root user:
ps -u root
Displaying group processes
Similar to listing user processes, listing group processes does the same. You can list a group process by giving the group name or group ID and specifying the ps command.
Run the following command, for instance, to see the processes connected to the group “apache”:
ps -fG group-name : $ ps -fG apache
Displaying processes using PID and PPID
Using the PID (Process ID) and PPID, Linux processes can also be shown (parent process ID). The parent PID of a process is referred to as the PPID.
Run to list all processes by PID:
ps -fp PID
Searching for a PID using process name
Most of the time, we are unaware of the PID associated with the process name. For this, use the -C flag as displayed.
ps -C process-name
Displaying process using TTY
Use the -t flag to filter processes based on their tty values. Use the ps command as demonstrated to, for instance, list the processes connected to tty1:
ps -ft tty1
Printing the Process Tree
A process tree demonstrates how connected processes are in a Linux system. Processes without parents connecting to the system via the system or init. You must use the following format for printing:
ps -e –forest
Real-time Process Monitoring
The watch utility lets you keep an eye on a process’s static data. The output is displayed below and plays every second.
watch -n 1 'ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head'
Troubleshoot Performance Issues of Linux Machine
The process with the highest CPU and memory use on a slow or overloaded system can be identified.
Run the following ps command to list the top processes using a lot of RAM.
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
Run the following command to list the top processes using a lot of CPU power.
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head
Killing unresponsive processes
Using the ps command, you can terminate any processes or applications using excessive CPU or RAM. To accomplish this, you must use the command to discover the PID of that specific application:
ps -A | grep -i stress
Display the duration that processes have been running
Run the following command to see the duration of any running processes:
ps -eo comm,etime,user
Using ‘ps’ or ‘top’ from the terminal to display process information is easy in Linux. The level of detail you wish to see for the active or inactive processes on your system is up to you. Look around and use Google searches and the man pages for further information.