Linux Command pstree Usage and 5 Options

The Linux command pstree visually represents processes that are running in a parent-child relationship. As the name suggests, pstree displays the process tree, providing a hierarchical view of currently running processes. In this post, we will explore how to use the pstree command, along with its main options and practical usage.

What is the Linux Command pstree?

pstree is a command that shows all currently running processes in a tree format. Unlike the ps command, pstree allows you to clearly see the parent-child relationships of processes at a glance.

What is a Process Tree?

In Linux, every process has a parent process and, possibly, child processes. For example, when the system boots up, the first process is init or systemd, which is the parent process. This parent process then spawns multiple child processes. The structure of these relationships is visualized in the form of a tree, known as a process tree.

Basic Usage of the pstree Command

By simply typing the pstree command in the terminal, you can display the currently running process tree. Here is the most basic usage:

pstree
ShellScript

After running this command, you will see the process tree of your system displayed in a hierarchical format, where child processes are listed under their respective parent processes.

Figure 1. Default output of the Linux command pstree
Figure 1. Default output of the Linux command pstree

As shown above, systemd is the parent process, and several child processes are listed under it in a tree format.

Viewing Processes for a Specific User

To view only the processes of a specific user, you can use the [username] option. This is particularly useful in large systems where you may want to track processes for a particular user.

pstree username
ShellScript

This command shows only the processes currently running by the specified user in a tree structure, as shown in the figure below.

Figure 2. Linux command pstree: Showing processes for user ito
Figure 2. Linux command pstree: Showing processes for user ito

Viewing Processes for a Specific PID

If you want to see processes for a specific PID (Process ID), you can pass the PID value as a parameter to the pstree command.

pstree PID
ShellScript

To quickly check both the structure and the PID, you can combine this with the -p option.

Figure 3. Linux command pstree: Showing processes for PID 838
Figure 3. Linux command pstree: Showing processes for PID 838

Key Options

pstree offers various options to refine the output or make it easier to read. Below are some of the most frequently used options.

Viewing Processes by User (-u)

By using the -u option, you can see which user has executed each process.

pstree -u
ShellScript

This command adds the username next to each process, making it easy to identify which user is running which processes.

Figure 4. Linux command pstree: Displaying user information with -u option
Figure 4. Linux command pstree: Displaying user information with -u option

Displaying PIDs (-p)

If you need to check the Process ID (PID), use the -p option. The PID is a unique identifier assigned to each process.

pstree -p
ShellScript

As shown in the figure below, the PID of each process is displayed in parentheses.

Figure 5. Linux command pstree: Displaying PIDs with the -p option
Figure 5. Linux command pstree: Displaying PIDs with the -p option

Highlight Mode (-H)

The default output of pstree doesn’t highlight any processes, making it harder to locate specific ones. The -H option allows you to highlight a specific PID.

pstree -H PID
ShellScript

The figure below shows the process with PID 838 highlighted.

Figure 6. Linux command pstree: Highlighting a specific PID with the -H option
Figure 6. Linux command pstree: Highlighting a specific PID with the -H option

ASCII Mode (-A) and Unicode Mode (-U)

By default, pstree outputs in Unicode mode. You can explicitly select Unicode mode, or if you prefer to see the process tree in ASCII characters, use the -A option.

pstree -U  # unicode mode
pstree -A  # ASCII mode
ShellScript

Below is an example of how the process tree is displayed using ASCII mode.

Figure 7. Linux command pstree: Displaying the process tree in ASCII mode with the -A option
Figure 7. Linux command pstree: Displaying the process tree in ASCII mode with the -A option

Practical Usage Examples

System Monitoring

The pstree command is very useful for system monitoring and troubleshooting. For example, if a particular process unexpectedly spawns too many child processes, you can easily identify this using pstree.

Daemon Management

In a Linux server environment, where many daemons are running, you can quickly view the child processes of each daemon in the tree structure, making it easier to trace issues when something goes wrong.

Caution

  • When there are many processes running, the tree structure can become quite complex. In such cases, it’s recommended to filter the output by using options like PID or user-specific processes.
  • In some Linux distributions, the pstree command may not be installed by default. If it’s missing, you can install the psmisc package.

Summary

The pstree command provides a visual representation of processes running on a Linux system, helping you manage system monitoring and troubleshooting more intuitively. By clearly displaying the parent-child relationships of processes, it is particularly useful for managing system resources and processes. With its various options, you can customize the output according to your needs.

Use this command to effectively manage processes even in complex system environments.

References

Leave a Comment