LINFO

The pstree Command



pstree is a small, command line (i.e., all-text mode) program that displays the processes (i.e., executing instances of programs) on the system in the form of a tree diagram. It differs from the much more commonly used (and more complex) ps program in a number of respects, including that the latter shows the processes in a list rather than a tree diagram but provides more detailed information about them.

Tree Diagrams

A tree diagram is a way of showing the ancestral relationships among processes (or other entities) by connecting them with short lines that indicate for each process the process from which it originated (i.e., its parent) and any processes that it created (i.e., its children). This type of diagram differs from the usual image of a tree in that the root is at the top and the branches point downwards.

Similar inverted tree diagrams are commonly used to illustrate the hierarchical filesystems of Linux and other Unix-like operating systems, which begin with the root directory (represented by a forward slash) off from which branch the second tier directories such as /bin, /boot, /etc, /home, /mount and /sbin.

One of the advantages of pstree as compared with ps is that it makes it easier to terminate a series of related processes (i.e., all of the descendants of a particular process). This is because pstree makes it immediately clear which process is the parent, and all that is necessary is to terminate the parent in order to extinguish all of its descendant processes. That is, it is not necessary to manually search through a list to find and individually terminate each process as would be necessary using ps. The kill command is commonly used to terminate a crashed or otherwise misbehaving program or process.

Syntax

The basic syntax for pstree is:

pstree [options] [pid or username]

The square brackets indicate that the items in them are optional. If pstree is used without any options or arguments, that is, by typing

pstree

and then pressing the ENTER key, the result is a tree diagram that shows all of the processes currently on the system.

At the very top of the diagram is the process init. This is because init is always the first process that is started when Linux is booted up (i.e., started up). It is the ancestor of all other processes, and it remains on the system for the duration of the session. It can be seen that pstree itself is also listed as a process, as is everything on the system.

The processes that are directly connected to the main stem (i.e., a vertical line extending downward from init along the left hand edge of the screen) of the tree are listed by default in alphabetic order. This is in contrast to ps, which by default lists the processes in the sequence in which they were created.

In order to economize on screen space and thus make its output easier to read, pstree visually merges identical branches by placing them in square brackets and prefixing them with an integer indicating the original number of branches. For example, four identical branches of nautilus representing four instances of this program would be compacted into a single branch labeled -4*[nautilus] rather than being shown as four separate branches. (Nautilus is the official file manager for the GNOME desktop, which is used on Red Hat and many other distributions of Linux.)

Because the tree diagram is generally long even with compaction and can easily require more vertical space than is available on a single screen, it can be convenient to use it together with the less command, i.e.,

pstree | less

This allows the output to be viewed one screenful at a time. The output can be advanced by pressing the space bar, and previous screens can be returned to by pressing the b key.

Options

As is the case with most command line programs, pstree has several options that greatly enhance its flexibility and usefulness. One of the most commonly used is -p, which tells it to show process identification numbers (PIDs). A PID is a unique, identifying, non-negative integer that is automatically assigned to each process on a Unix-like operating system. The command

pstree -p

will tell pstree to show a PID in parenthesis after each process name. ps, in contrast to pstree, shows PIDs by default.

The -p option has a side effect of disabling compaction. This is because compaction occurs only for lines of output that are identical. Lines that are identical when no PIDs are shown are no longer identical when the PIDs are shown because each instance of a program (and thus each line) has a different PID. An alternative is to use the -c option, whose sole purpose is to disable compaction.

The -n option instructs pstree to sort its output in the order of the PIDs instead of in the default alphabetic order. This can be seen most clearly when using it together with the -p option, i.e.,

pstree -np | less

The -h option highlights (or shows in bold) the current process and all of its ancestors, if highlighting is permitted by the particular system. For example, on a Red Hat 9 system, this option results in something such as gnome-terminal-bash-pstree as well as init (because it is the ancestor of all processes) being shown in bold characters.

The -u option tells pstree to show the owner of a process (i.e., the user that started it) in parenthesis whenever its ownership differs from that of its parent. This makes it a simple matter to determine who is the owner of any process.

The -l option prevents the truncation of long lines of output that can occur when the display screen is not sufficiently wide. It does this by wrapping (i.e., carrying over) any excess output to the next line.

The -a option shows the command line arguments for each process that was initiated by a user (instead of by another process) by issuing a command with arguments. As is the case with the -p option, this option also automatically disables compaction.

Arguments

An argument, also called a command line argument, is a file name or other information that is provided to a command in order for that command to use it as an input. pstree can accept a maximum of one argument, and that argument can be either the name of a user or a PID.

Providing pstree with an argument causes it to show only parts of the process tree rather than the entire tree. If a user name is specified, only the branches that start at processes owned by that user are shown. If a PID is provided, only the branch that begins with that process is shown.

For example, the following command will show only those branches that have been initiated by a user with a username joe:

pstree joe

It can be seen that the main stem (i.e., the vertical stem) is not shown when the name of an ordinary user is provided as an argument. However, the main stem is shown if the argument is the root user (i.e., administrative user), as can be seen by entering the following:

pstree root

The reason is that init is owned by the root user (which can be verified by using the ps -aux command).






Created September 23, 2004. Updated September 24, 2005.
Copyright © 2004 - 2006 The Linux Information Project. All Rights Reserved.