Skip to content

ps command in linux and examples that are used in practice

Introduction

In the world of Linux, process management is a vital skill for any user, whether you’re an administrator, developer, or power user. One of the most essential and widely used tools for process management is the ps(Process Status) command. This powerful utility allows you to monitor and manage running processes, helping you keep track of system resources and identify potential issues. In this post, we’ll explore the ps command, covering its basic usage, various options, and practical examples to enhance your Linux process management skills.

When ps command is needed?

In the world of servers, multiple users often access the system simultaneously, leading to various processes running in the background. You might encounter situations where the server suddenly slows down even though you haven’t made any changes. In such cases, you can use the top (or topas) and ps -ef commands to check CPU usage and monitor active processes.

Another scenario where the ps command comes in handy is when you’re running a script and it appears to be stuck in the middle of execution. You might be unsure whether the script has crashed, is still running, or has encountered a deadlock. The ps command helps you check if the process is still active and, if necessary, forcefully terminate it.

To be honest, knowing the ps -ef command is often sufficient, as it’s commonly used in various situations. However, let’s seize this opportunity to explore the diverse options and examples of the ps command, enhancing our understanding and proficiency in process management.

ps command

The ps command is used to display a list and status of currently running processes on the system.

The options for the ps command vary depending on the Unix tradition it belongs to, such as System V, BSD, and GNU. The output and notation can differ across these traditions. When using options, System V notation employs a dash (-), while BSD notation does not use a dash. In GNU notation, two dashes (–) are used. Therefore, it’s essential to use the correct options to display the desired process status.

For example, there is a distinction between the ‘a’ and ‘-a’ options:

As seen in the man ps documentation, there are separate entries for ‘a’ and ‘-a’. It’s crucial to understand the differences in notation, as failing to do so may lead to confusion and potential issues later on.

Basic Usage and Option

This is the usage of the ps command:

$ ps [option]

Although there are differences between BSD and System V notations, options from one tradition are not entirely unusable in the other. As mentioned earlier, the notation varies, and each operating system might support slightly different options. Let’s look at some common options, and for more details, refer to the man ps command.

By using ps –help, you can see many options, but there are too many to explain them all. In this post, we’ll focus on basic options and those frequently used in practice.

OptionsDescription
-A, -eShow all processes, including those without a controlling terminal
-aDisplay processes with a controlling terminal, for all users
aShow processes for all users, including those without a controlling terminal
-dList all processes, but exclude session leaders
-N, –deselectShow processes that do not match the specified selection criteria
rList only running processes
TDisplay processes associated with the current terminal
xInclude processes without a controlling terminal
uShow processes owned by the specified user, with additional details
-eShow environment details for processes
-fShow extended process information, including command-line arguments
-pDisplay information for the specified process ID(s)
-u, U, –userShow processes owned by the specified user, with additional details
-U, –UserDisplay processes owned by the specified user, but with different output format than -u, U, –user
-oShow user-defined output format
-CDisplay processes with the specified command name

The ps command is often used in conjunction with the grep command to check for specific processes.

Meaning of Each Field

When you use the ps command alone, like this:

root@6d50ffee3072:/# ps
  PID TTY          TIME CMD
    1 pts/0    00:00:00 bash
    9 pts/0    00:00:00 ps

It displays processes related to the current user. The ps command is typically used in combination with options, rather than on its own.

By default, the output consists of four pieces of information: PID, TTY, TIME, and CMD.

PID represents the process ID, and TTY refers to the terminal connected to the process. This is just basic information, and more detailed content can be displayed depending on the options used.

root@6d50ffee3072:/# ps -el
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0     1     0  0  80   0 -  1157 do_wai pts/0    00:00:00 bash
0 R     0    11     1  0  80   0 -  1765 -      pts/0    00:00:00 ps

As shown in the output above, you won’t be able to understand the data without knowing the meaning of each field, like what PID or PPID represents. Let’s summarize the representative information that can be obtained with the ps command.

NameDescription
USERThe user who owns the process
UIDThe user ID of the process owner
PIDThe process ID
PPIDThe parent process ID
%CPUPercentage of CPU used by the process
%MEMPercentage of memory used by the process
VSZVirtual memory size of the process in kilobytes
RSSResident set size (physical memory used) in kilobytes
TTYTerminal associated with the process
STATProcess status (e.g., running, sleeping, zombie)
STARTStart time of the process
TIMECumulative CPU time used by the process
COMMANDThe command that started the process
ARGSCommand line arguments of the process
FFlags associated with the process
SProcess state (e.g., running, sleeping, stopped)
CProcessor utilization for scheduling
PRIProcess priority
NINice value of the process (affecting priority)
ADDRMemory address of the process
WCHANAddress of the kernel function where the process is sleeping

ps command examples that are frequently used in practice

ps ax

root@909157f0fb7d:/# ps ax
  PID TTY      STAT   TIME COMMAND
    1 pts/0    Ss     0:00 /bin/bash
   16 pts/0    R+     0:00 ps ax

This command displays all running processes on the system, regardless of the user or terminal associated with them. It combines the ‘a’ option, which shows processes for all users, and the ‘x’ option, which includes processes without a controlling terminal.

ps aux

root@909157f0fb7d:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   4628  3816 pts/0    Ss   06:13   0:00 /bin/bash
root        17  0.0  0.0   7060  1564 pts/0    R+   08:40   0:00 ps aux

This command lists all running processes with additional details, such as CPU and memory usage, for every user. The ‘u’ option displays user-oriented information, while the ‘a’ and ‘x’ options, as mentioned earlier, show processes for all users and those without a controlling terminal, respectively.

ps -ef

root@909157f0fb7d:/# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 06:13 pts/0    00:00:00 /bin/bash
root        18     1  0 08:41 pts/0    00:00:00 ps -ef

Using the ‘e’ and ‘f’ options, this command provides a full listing of all processes, including their PIDs, PPIDs, user and group ownership, start time, and the command that launched them. The ‘e’ option selects all processes, and the ‘f’ option generates a full-format listing.

ps -el

root@909157f0fb7d:/# ps -el
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0     1     0  0  80   0 -  1157 do_wai pts/0    00:00:00 bash
0 R     0    19     1  0  80   0 -  1765 -      pts/0    00:00:00 ps

This command displays an extended-format listing of all processes, providing more in-depth information about each process. The ‘e’ option selects all processes, and the ‘l’ option generates a long-format listing with additional fields.

ps -fp <PID>

root@909157f0fb7d:/# ps -fp 827
UID        PID  PPID  C STIME TTY          TIME CMD
root       827     1  0 08:44 ?        00:00:00 nginx: master process nginx

The ‘f’ and ‘p’ options are used here to display information about a specific process based on its process ID (PID). The ‘f’ option generates a full-format listing, while the ‘p’ option filters the output to show only the process with the specified PID.

ps -U <username> -u <username>

root@909157f0fb7d:/# ps -U root -u root
  PID TTY          TIME CMD
    1 pts/0    00:00:00 bash
  827 ?        00:00:00 nginx
 3199 pts/0    00:00:00 ps

This command lists processes owned by a specific user. The ‘U’ option selects processes with the specified real user ID (RUID), while the ‘u’ option filters the output based on the specified effective user ID (EUID). In most cases, the RUID and EUID will be the same, but they can differ in certain situations, such as when using the ‘sudo’ command.

ps -t <tty>

root@909157f0fb7d:/# ps -t pts/0
  PID TTY          TIME CMD
    1 pts/0    00:00:00 bash
 3200 pts/0    00:00:00 ps

This command displays processes associated with a specific terminal (tty). The ‘t’ option filters the output based on the specified tty, making it useful for identifying processes running in a particular terminal session.

ps -e –forest

root@909157f0fb7d:/# ps -e --forest
  PID TTY          TIME CMD
    1 pts/0    00:00:00 bash
  827 ?        00:00:00 nginx
  828 ?        00:00:00  \_ nginx
  829 ?        00:00:00  \_ nginx
  830 ?        00:00:00  \_ nginx
  831 ?        00:00:00  \_ nginx
  832 ?        00:00:00  \_ nginx
  833 ?        00:00:00  \_ nginx
  834 ?        00:00:00  \_ nginx
  835 ?        00:00:00  \_ nginx
  836 ?        00:00:00  \_ nginx
  837 ?        00:00:00  \_ nginx
  838 ?        00:00:00  \_ nginx
  839 ?        00:00:00  \_ nginx
  840 ?        00:00:00  \_ nginx
  841 ?        00:00:00  \_ nginx
  842 ?        00:00:00  \_ nginx
  843 ?        00:00:00  \_ nginx
 3202 pts/0    00:00:00 ps

The ‘e’ option selects all processes, while the ‘–forest’ option generates a hierarchical tree-like representation of process relationships, showcasing parent and child processes. This visual representation makes it easier to understand the connections between different processes.

ps -e -o pid,ppid,uname,pcpu,pmem,comm,tty –sort=-%cpu

root@909157f0fb7d:/# ps -e -o pid,ppid,uname,pcpu,pmem,comm,tty --sort=-%cpu
  PID  PPID USER     %CPU %MEM COMMAND         TT
    1     0 root      0.0  0.0 bash            pts/0
  827     1 root      0.0  0.0 nginx           ?
  828   827 www-data  0.0  0.0 nginx           ?
  829   827 www-data  0.0  0.0 nginx           ?
  830   827 www-data  0.0  0.0 nginx           ?
  831   827 www-data  0.0  0.0 nginx           ?
  832   827 www-data  0.0  0.0 nginx           ?
  833   827 www-data  0.0  0.0 nginx           ?
  834   827 www-data  0.0  0.0 nginx           ?
  835   827 www-data  0.0  0.0 nginx           ?
  836   827 www-data  0.0  0.0 nginx           ?
  837   827 www-data  0.0  0.0 nginx           ?
  838   827 www-data  0.0  0.0 nginx           ?
  839   827 www-data  0.0  0.0 nginx           ?
  840   827 www-data  0.0  0.0 nginx           ?
  841   827 www-data  0.0  0.0 nginx           ?
  842   827 www-data  0.0  0.0 nginx           ?
  843   827 www-data  0.0  0.0 nginx           ?
 3205     1 root      0.0  0.0 ps              pts/0

This command lists all processes with customized output fields and sorts them by descending CPU usage. The ‘e’ option selects all processes, while the ‘-o’ option specifies the output fields: PID, PPID, user name, CPU usage, memory usage, command name, and tty. The ‘–sort’ option orders the output by the specified criterion, in this case, CPU usage.

ps -p <PID> -o comm=

root@909157f0fb7d:/# ps -p 827 -o comm=
nginx

This command displays the command name (comm) for a specific process, identified by its process ID (PID). The ‘p’ option filters the output based on the specified PID, while the ‘-o’ option customizes the output to show only the command name without a header.

ps -C <command_name> -o pid=

root@909157f0fb7d:/# ps -C nginx -o pid=
  827
  828
  829
  830
  831
  832
  833
  834
  835
  836
  837
  838
  839
  840
  841
  842
  843

The ‘C’ option filters the output to show processes with the specified command name, while the ‘-o’ option customizes the output to display only the process IDs (PIDs) without a header. This command is useful for quickly finding the PIDs of processes associated with a specific command.

Conclusion

In this article, we explored the Linux ‘ps’ command, its various options, and some of the most common use cases in practice. the ‘ps’ command offers many more options and possibilities than what we covered in this article. To dive deeper into its capabilities, consult the ‘man ps’ command for a complete list of options and their descriptions. With time and practice, the ‘ps’ command will become an indispensable tool

Tags:
child neve