Menu

3 Linux ps command tips/tricks for advanced users

The ps command in Linux, as you might already know, is one of the most frequently used command line tools. For the uninitiated, the utility basically reports a snapshot of the current processes on the system.

The ps command provides a plethora of options, and as you might agree, it's not possible to know about or practice each and every feature that it offers. So, in this article, we will quickly discuss three ps-related tips/tricks that advanced users may find useful.

Before we move ahead, it's worth mentioning that all the commands and instructions present in this tutorial have been tested on Ubuntu 16.04 LTS running bash version 4.3.46. The ps command version used is: 3.3.10.

1. Show processes in a hierarchy

If information can be presented in a graphical form, it becomes easy to comprehend and remember. If you agree with this, you'll be glad to know that's possible with the ps command as well, as the tool is capable of showing processes in hierarchical format.

The feature in question can be accessed using the --forest command line option. Here's an example for it:

ps -ae --forest

Following is a small part of the output the above command produced on my system:

So that should given you a good idea about the kind of output the ps command produces when the --forest option is supplied to it.

Note: The ps command does not provide a direct way to display only a particular process (and its sub-processes) in hierarchical format. However, that doesn't mean it's not possible - some solutions to this problem are mentioned here.

2. Show threads

If you are only interested in having information related to threads corresponding to a particular process, you can use the -L command line option. Here's an example:

So you can see in the screenshot above, all threads for the process with PID 4103 were displayed in the output.

3. Run ps in dynamic mode

As mentioned in the beginning, the ps command only produces a snapshot of processes, meaning the information produced is static. In case you want the command to be run in a way that makes sure the output gets updated periodically, then you can pass your ps command as an argument to the watch command.

Here's an example:

watch -n 2 'ps -aef | grep firefox'

So the above command will make sure that the output of the ps -aef | grep firefox command gets updated after every 2 seconds.