‘ps’ Command to Monitor Linux Process – Introduction
‘ps’ Command to Monitor Linux Process is an essential tool for system administrators and developers. The ‘ps’ command by default displays details about the processes that are active in the current shell session. This information can be useful for troubleshooting, optimizing performance, and identifying potential security issues. However, it can be used to list information about all processes running on the system, regardless of whether they were started in the current shell session or not.
The basic syntax of the ‘ps’ command
The ‘ps’ command’s fundamental syntax is as follows:
ps [options]
Here, the various options and arguments can be used with the ‘ps’ command to customize the output and filter the processes based on various criteria.
The ‘ps’ command can be used with a variety of options and arguments to display different types of information about running processes on the system. Here are some of the most used options and arguments:
-a: This switch instructs ‘ps’ to show details about all processes connected to the active terminal.
-e: This option instructs “ps” to show details about every process currently active on the system, not simply those connected to the active terminal.
-f: This option instructs ‘ps’ to show a full-format listing of all running processes, along with the user who initiated each one, the command that launched it, and other information.
-u: This option instructs ‘ps’ to show details about processes that belong to the given user.
-p: This option instructs ‘ps’ to show details about the process identified by the supplied PID.
Some many other options and arguments can be used with the ‘ps’ command, depending on the specific information you want to obtain about running processes on the system.
Here are some of the most used ‘ps’ commands.
-
Display a list of all processes
You can use the ‘ps’ command with the -e option to display a list of all processes, which instructs it to show details about all processes currently active on the system:
ps -e
This will provide a list of all active processes, their process IDs (PIDs), the user who launched them, the command that launched them, and other details.
By default, the ps command will display a truncated output, with some columns removed to fit the output on a single line. To display a full-format listing of processes, you can use the -f option, like this:
ps -ef
This will display a detailed output that includes the full command line used to start each process, along with other information.
-
List all processes and their whole command lines
Use the command below to show a list of all processes together with their complete command lines.
ps auxf
All processes will be shown in a tree-like layout, making it simpler to understand their relationships.
-
List all processes in order of CPU consumption
Use the following command to display a list of all processes sorted by CPU consumption.
ps aux –sort=-%cpu
This will display a list of all processes, including system processes and user processes, sorted in descending order based on CPU usage.
You can also use the top command to continuously monitor CPU usage and sort by CPU usage. To launch the top, simply type top in the terminal. Once it is launched, you can sort by CPU usage by pressing the Shift and P keys together.
-
List all processes in alphabetical order by memory use
Use the following command to provide a list of all processes sorted by memory usage:
ps aux –sort=-%mem
This will display a list of all processes, including system processes and user processes, sorted in descending order based on memory usage.
-
Show a list of all processes that belong to a certain user
You can use the following command to show a list of all processes controlled by a certain user:
ps -u username
Substitute the name of the user whose processes you want to see for the username. This will display a list of every process that the user owns.
Use the aux option as follows to use additional comprehensive information about the processes:
ps aux -u username
This will display more specific data about the processes that belong to the selected user.
-
Show a complete list of all processes for a certain command
Use the following command to show a list of all processes for a particular command:
ps -C command
Replace the command name with the name of the command whose processes you want to display. This will show a list of all processes that are currently running for the specified command.
-
Provide a list of all processes that are active in a certain terminal
Use the “ps” command with the “t” option and the terminal name to display a list of all processes currently active in that terminal. Here’s the command:
ps t <terminal_name>
Replace <terminal_name> with the name of the terminal you want to check. For example, if you want to display a list of all processes running in the terminal named “tty1”, you can use the following command.
ps t tty1
This will list every process that is currently active on the “tty1” terminal. The list will include the process ID (PID), the terminal name, the status of the process, the CPU and memory usage, and the command used to launch the process.
-
Show a list of every process in a particular process group.
ps -C <process_name>
The ps -C command is used to display a list of all the processes with a specific name. This command is often used for process-specific monitoring and management. Put the name of the command you wish to observe processes for instead of “command.”
-
Show a list of every process in a particular process group
Use the following command to display a list of every process in a certain process group.
ps –pid paid
Change “pgid” to the process group ID of the group whose processes you wish to see.
-
For a given parent process, provide a list of all the children’s processes
To display a list of all child processes for a specific parent process, you can use the ps command with the –ppid option followed by the process ID of the parent process. Here’s an example:
ps –ppid <parent_process_id>
Replace <parent_process_id> with the ID of the parent process you want to view. The –no-headers option is used to hide the header row of the output.
This command will list all the child processes of the specified parent process, including their process IDs, status, and other information. The output will be similar to the standard ps command output, but will only include processes with the specified parent process ID as their parent process.
-
Show all processes with a certain name in a list
ps -C <process_name>
The ps -C command is used to display a list of all the processes with a specific name. This command is often used for process-specific monitoring and management.
-
List all zombie processes on the screen
Use the command below to display a list of all zombie processes.
ps aux | awk ‘$8==”Z” {print}’
Processes that have ceased running but haven’t been fully cleaned up by the system are referred to as zombie processes.
-
Show a list of all processes that are in a certain condition
To display a list of all processes in a specific state, you can use the ps command with the –state option followed by the state code. Here are some examples:
To display a list of all running processes:
ps –state R
To display a list of all sleeping processes:
ps –state S
To display a list of all stopped processes:
ps –state T
To display a list of all uninterruptible sleep processes:
ps –state D
To display a list of all traced or stopped processes:
ps –state Tt
Note that the state codes are case-sensitive, and you can combine them to show processes in multiple states at the same time. Also, some states such as Z (zombie) and X (dead) are not listed by default, so you need to specify them explicitly using the –state option.
-
List all processes with a certain priority will be shown
Use the following command to display a list of all processes with a certain priority.
ps -o pid,ppid,user,nice,cmd –sort=-nice
This will show a list of every process, arranged in decreasing order of importance.
-
Show a list of all processes that are utilizing a certain port
Use the command below to see a list of all processes that are utilizing a certain port.
sudo lsof -i :port
Put the port number you wish to observe processes for instead of “port”
-
Show a list of all processes that are active under a certain user
Use the ps command with the -U parameter and the specified username to display a list of all processes that are currently running as that particular user. Here’s an example:
ps -U username
This will list all processes currently running on the system for the specified user. If you want more detailed information about the processes, you can add the f flag to display them in a tree structure:
ps -U username f
This will show the processes in a hierarchical structure, with the parent process at the top and the child processes listed below. You can also use other flags to customize the output, such as aux to show additional information like the process owner, CPU usage, and memory usage:
ps -U username aux
-
Show a list of all processes that are active in a particular directory
Use the ls command with the -c and -d arguments to display a list of all processes that are active in a particular directory. Here’s an example:
lsof -c ” -d cwd /path/to/directory
This will list all open files of all processes (-c ” specifies no process name) that are currently running with their current working directory (-d cwd) set to the specified directory (/path/to/directory).
Alternatively, you can use the fuser command with the -m flag to list all processes using a specific file system:
fuser -m /path/to/directory
This will show all processes using the file system that contains the specified directory. Note that this command will also display processes that may not necessarily be running within the directory but are using resources associated with the file system containing the directory.
Wrapping Up
The ‘ps’ command may show a lot of details about the processes that are currently active on the system, such as the process ID (PID), the parent process ID (PPID), the user who launched the process, the command that launched the process, and many other things.
You can learn about linux more deeply by clicking the link below
https://linuxiron.com/what-is-linux-a-whole-introduction/
Learn about the other linux commands by clicking the links below
https://linuxiron.com/echo-command-in-linux/
https://linuxiron.com/how-to-use-nice-renice-commands-in-linux/
https://linuxiron.com/how-to-use-kill-commands-in-linux/
https://linuxiron.com/a-beginners-guide-to-htop-for-process-management/
https://linuxiron.com/15-useful-yum-commands-in-linux/
https://linuxiron.com/how-to-use-the-top-command-in-linux/
When the -m flag is used, the ‘ps’ command also prints the corresponding kernel threads to standard output along with the status of the currently running processes. The THREAD field specifier must be used in conjunction with the -o flag to display additional thread-related columns, whereas the -m flag displays threads connected to processes using extra lines.
The fact that processes eat up system resources like CPU time, memory, and disc space necessitates their management and monitoring. Moreover, security and safety issues are raised. Systems administrators, therefore, play a crucial role in monitoring and managing processes.
Ps stands for “Process Status” in an acronym. In Linux-based operating systems, it is a command-line program that aids in examining data about several active processes on a system.
Several of the issues with quality assurance and testing in additive manufacturing can be resolved by in-process monitoring. Before your print even leaves the print bed, you already have a comprehensive report thanks to the ability to track it and collect data on it layer by layer.