Skip to main content

Process Handling

Show Process Tree in Linux

Here are multiple commands that you can use to show the process tree in the Linux command line.

So you used the killall command and it killed the parent process making your hours of work at waste?

Well, I went through the same so it is always a better idea to check the parent processes, and listing the ongoing processes in tree manner is a good idea.

Use the ps command to show the process tree

The ps command in Linux is used to find ongoing processes in Linux and it also avails you to print the exact info in a tree manner.

You can use the --tree option to show the process tree with the ps command:

ps -ef --forest

Here,

  • -e is used to select every process.
  • -f gets details in full format.

Use the pstree command to show the process tree

Personally, this is what I prefer to have as even without any options, it works fine.

But your distro may not have it pre-installed and if you're on Ubuntu-based distros, the given command should do it:

sudo apt install psmisc

Now, you can simply use the pstree command and it should give you the following output:

pstree

Similarly, you can use -p option to get process IDs:

pstree -p

Use the tree command to show the process tree

While the tree command is mainly used to list files recursively, you can look at /proc to get the process tree.

But it requires manual installation as it does not come pre-installed and if you're on an Ubuntu-based distro, the following command should do it:

sudo apt install tree

Now, you can use the given command to show the process tree:

tree /proc

It showed 23665 directories and 435044 files which bloated my terminal window and you can limit the info using -d option as it will only show directories:

tree -d /proc

And if you want to have full pathnames, you can use -f option:

tree -f /proc

Use the htop utility to show the process tree

htop is similar to the top command. And in my opinion, this is arguably the most interactive way of checking ongoing processes in the terminal.

But what if I tell you that it is also capable of showing a process tree too? But before that, let's head over to the installation part.

It does not come pre-installed and if you are on an Ubuntu-based distro, the given command should do it:

sudo apt install htop

Once you are done with the installation, use the given command to start htop:

htop

Now, you just have to press F5 and the ongoing process will be shown to you in tree format:

Wrapping Up

One practical example of displaying the process tree is in checking whether your system uses systemd or not.

This was my take on how you can show the process tree using various utilities but there is a lot more you can do with those utilities. More on them later.

To learn more, you can always use the man page to make the most out of utilities.

Sagar Sharma