Ps Command in Linux (List Processes)

Posted 

5 min read

In Linux, a running instance of a program is called process. Occasionally, when working on a Linux machine you may need to find out what processes are currently running.

There are number of commands that you can use to find information about the running processes, with ps and top being the most commonly used ones.

In this article, we will talk about how to use the ps command to list the currently running processes and display information about the about those processes.

How to Use ps Command

The general syntax for the ps command is as follows:

ps [OPTIONS]

For historic and compatibility reasons the ps command accepts several different types of options:

  • UNIX style options, preceded by a single dash.
  • BSD style options, used without a dash.
  • GNU long options, preceded by two dashes.

All types of options can be mixed but in some special cases conflicts can appear, so it is best to stick with one type of options. BSD and UNIX options can be grouped.

In it’s simplest form when used without any option, ps will print four columns of information for minimum two processes running in the current shell, the shell itself and the processes that run in the shell when the command.

ps

The output includes information about the shell (bash) and the process running in this shell (ps, the command that you typed):

  PID TTY          TIME CMD
 1809 pts/0    00:00:00 bash
 2043 pts/0    00:00:00 ps

The four columns are labeled PID, TTY, TIME and CMD.

  • PID - The process ID. In most situations when running the ps command the most important information the user is looking for is the process PID. Knowing the PID allows you to kill a malfunctioning process.
  • TTY - The name of the controlling terminal for the process.
  • TIME - The cumulative CPU time of the process, shown in minutes and seconds.
  • CMD - The name of the command that was used to start the process.

The output above is not very useful as it doesn’t contain much information. The real power of the ps command comes when launched with additional options.

The ps command accepts a huge number of options that can be used to display a specific group of process and different information about the process, but only a handful are needed in day-to-day usage.

ps is most frequently used with the following combination of options:

BSD form:

ps aux
  • The a option tells ps the display the processes of all users, with except those processes that not associated with a terminal and processes of group leaders.
  • The u stands for a user-oriented format which provides detailed information about the processes.
  • The x option will case ps to list the processes without a controlling terminal. Those are mainly processes that are started on boot time and running in the background.

The command will display information in eleven columns labeled USER, PID, %CPU, %MEM, VSZ, RSS, STAT, START, TTY, TIME and CMD.

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.8  77616  8604 ?        Ss   19:47   0:01 /sbin/init
root         2  0.0  0.0      0     0 ?        S    19:47   0:00 [kthreadd]
...

We already explained PID, TTY, TIME and CMD labels. Here is an explanation of other labels:

  • USER - The user who runs the process.
  • %CPU - The process cpu utilization.
  • %MEM - The percentage of the process’s resident set size to the physical memory on the machine.
  • VSZ - Virtual memory size of the process in KiB.
  • RSS - The size of the physical memory that the process is using.
  • STAT - The the process state code, which can be Z (zombie), S (sleeping), R (running) ..etc
  • START - The time when the command started.

To print a process tree add the f option. This will tell ps to display a tree view of parent to child processes.

ps auxf

The ps command also allows you to sort the output. For example, to sort the output based on the memory usage you would use:

ps aux --sort=-%mem

UNIX form:

ps -ef
  • The e option tells ps to display all processes.
  • The f stands full-format listing which provides detailed information about the processes.

The command will display information in eleven columns labeled UID, PID, PPID, C, STIME, TIME and CMD.

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 19:47 ?        00:00:01 /sbin/init
root         2     0  0 19:47 ?        00:00:00 [kthreadd]
...

The labels that are not already explained have the following meaning:

  • UID - Same as USER, the user who runs the process.
  • PPID - The ID of the parent process.
  • C - Same as %CPU, the process cpu utilization.
  • STIME - Same as START, the time when the command started.

To see only the processes running as a specific user use the following command, where linuxize is the name of the user:

ps -f -U linuxize -u linuxize

User-defined Format

The o option allows you to specify which columns will be displayed when running the ps command.

For example, to print information only about the PID and COMMAND you would run one of the following commands:

ps -efo pid,comm

console-bash ps auxo pid,comm


## Using `ps` With Other Commands

`ps` can be used in combination with other commands through piping.  

If you want to display the output of the `ps` command, one page at a time pipe it to the [`less`](https://linuxize.com/post/less-command-in-linux/) command:

console-bash ps -ef | less


The output of the `ps` command can be filtered with [`grep`](https://linuxize.com/post/how-to-use-grep-command-to-search-files-in-linux/). For example, to show only the process belonging to the root user you would run:

console-bash ps -ef | grep root ```

Conclusion

The ps command is one of the most commonly used commands when troubleshooting issues on Linux systems. It has many options, but usually, most users are using either ps aux or ps -ef to gather information about running processes.

For information about all available ps options type man ps in your terminal.

If you have any questions or feedback, feel free to leave a comment.