25 Useful ‘ps Command’ Examples for Linux Process Monitoring


The ‘ps’ command is a popular tool used for monitoring processes in Linux operating system. It stands for ‘process status’ and is used to display information about processes currently running on a system. This information can be useful for troubleshooting, optimizing performance, and identifying potential security issues. In this article, we will discuss 25 useful ‘ps command’ examples for Linux process monitoring.

Display a list of all processes

To display a list of all processes running on a Linux system, use following command −

ps aux

This will display a list of all processes along with their PID (process ID), CPU usage, memory usage, and other relevant information.

Display a list of all processes with full command lines

To display a list of all processes along with their full command lines, use following command −

ps auxf

This will display a tree-like structure of all processes, making it easier to see how they are related.

Display a list of all processes sorted by CPU usage

To display a list of all processes sorted by CPU usage, use following command −

ps aux --sort=-%cpu

This will display a list of all processes sorted in descending order of CPU usage.

Display a list of all processes sorted by memory usage

To display a list of all processes sorted by memory usage, use following command −

ps aux --sort=-%mem

This will display a list of all processes sorted in descending order of memory usage.

Display a list of all processes owned by a specific user

To display a list of all processes owned by a specific user, use following command −

ps -u username

Replace ‘username’ with actual username of user you want to view processes for.

Display a list of all processes for a specific command

To display a list of all processes for a specific command, use following command −

ps -C command

Replace ‘command’ with name of command you want to view processes for.

Display a list of all processes running in a specific terminal

To display a list of all processes running in a specific terminal, use following command −

ps -t tty

Replace ‘tty’ with name of terminal you want to view processes for.

Display a list of all processes in a specific process group

To display a list of all processes in a specific process group, use following command −

ps --pid pgid

Replace ‘pgid’ with process group ID of group you want to view processes for.

Display a list of all child processes for a specific parent process

To display a list of all child processes for a specific parent process, use following command −

ps --ppid pid

Replace ‘pid’ with process ID of parent process you want to view child processes for.

Display a list of all processes with a specific name

To display a list of all processes with a specific name, use following command −

ps -C name

Replace ‘name’ with name of process you want to view.

Display a list of all zombie processes

To display a list of all zombie processes, use following command −

ps aux | awk '$8=="Z" {print}'

Zombie processes are those that have finished executing but have not been properly cleaned up by system.

Display a list of all processes in a specific state

To display a list of all processes in a specific state, use following command −

ps -eo state,pid,user,command | grep state

Replace ‘state’ with state you want to view (e.g. R for running, S for sleeping, Z for zombie).

Display a list of all processes with a specific priority

To display a list of all processes with a specific priority, use following command −

ps -o pid,ppid,user,nice,cmd --sort=-nice

This will display a list of all processes sorted in descending order of priority.

Display a list of all processes with a specific CPU affinity

To display a list of all processes with a specific CPU affinity, use following command −

ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm | grep <cpu>

Replace ‘<cpu>’ with CPU you want to view processes for.

Display a list of all processes using a specific port

To display a list of all processes using a specific port, use following command −

sudo lsof -i :port

Replace ‘port’ with port number you want to view processes for.

Display a list of all processes running as a specific user

To display a list of all processes running as a specific user, use following command −

ps -U username -u username u

Replace ‘username’ with name of user you want to view processes for.

Display a list of all processes running in a specific directory

To display a list of all processes running in a specific directory, use following command −

lsof +D /directory

Replace ‘/directory’ with path to directory you want to view processes for.

Display a list of all processes running a specific executable

To display a list of all processes running a specific executable, use following command −

ps -C executable_name

Replace ‘executable_name’ with name of executable you want to view processes for.

Display a list of all processes using a specific file

To display a list of all processes using a specific file, use following command −

lsof /path/to/file

Replace ‘/path/to/file’ with path to file you want to view processes for.

Display a list of all processes with a specific environment variable

To display a list of all processes with a specific environment variable, use following command −

ps -eo pid,user,args | grep -E '^ *PID|^ *[0-9]+ .*VARNAME=VALUE'

Replace ‘VARNAME’ with name of environment variable you want to view processes for, and ‘VALUE’ with its value.

Display a list of all processes using a specific network interface

To display a list of all processes using a specific network interface, use following command −

sudo lsof -i -n -P | grep <interface>

Replace ‘<interface>’ with name of network interface you want to view processes for.

Display a list of all processes with a specific user-defined format

To display a list of all processes with a user-defined format, use following command −

ps -eo pid,user,%cpu,%mem,command --sort=-%mem | head

This will display a list of all processes sorted in descending order of memory usage, using a user-defined format.

Display a list of all processes with a specific memory address

To display a list of all processes with a specific memory address, use following command −

sudo pmap -x pid | grep address

Replace ‘pid’ with process ID of process you want to view memory addresses for, and ‘address’ with memory address you want to view.

Display a list of all processes with a specific user-defined filter

To display a list of all processes with a user-defined filter, use following command −

ps -eo pid,user,%cpu,%mem,command --sort=-%mem | awk '{if ($3 >= 10.0) print $0;}'

This will display a list of all processes using more than 10% of CPU and memory, using a user-defined filter.

Display a tree view of all processes

To display a tree view of all processes, use following command −

pstree

This will display a hierarchical tree view of all processes, with parent process at top and child processes below.

Conclusion

The ‘ps’ command is a powerful tool for monitoring and managing processes in a Linux system. With examples provided in this article, you can easily use ‘ps’ command to view information about running processes, filter and sort output, and perform various other operations. By mastering ‘ps’ command, you can improve performance and stability of your Linux system, and troubleshoot problems more effectively.

Updated on: 28-Mar-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements