Linux/Unix: pstree Command Examples: See A Tree Of Processes

by on February 5, 2013 · 3 comments· LAST UPDATED February 6, 2014

in , ,

pstree command

I am a new Linux user. How do I display the process on the Linux based server or desktop/laptop in easy to read tree format using bash shell prompt?

Linux and Unix are multitasking operating systems i.e. a system that can run multiple tasks (process) during the same period of time. A process is nothing but a running program (command) on Linux or Unix-like systems.

pstree command details
DescriptionSee running processes in the
form of a tree diagram
Category
Difficulty
Root privilegesNo
Estimated completion time10m
Contents
The pstree command shows running processes as a tree.

Purpose

Display running processes as a tree on Linux

Syntax

The basic syntax is as follows:

pstree

OR

pstree pid

OR

pstree user

OR

pstree [options] pid|user

NOTE: If pid given as a command line argument, start at this PID; default is 1 (init). if user given as a command line argument, show only trees rooted at processes of this user. pstree only accept one argument, and that argument can be either the user or a pid.

Install pstree on Unix

By default the pstree command installed and available on Linux operating systems only. For Unix-like system such as FreeBSD, OS X and others you need to install it using either source code or third party binary repos. You can compile pstree under most unixes, tested are AIX, Linux, HP-UX, A/UX, SunOS, Solaris, (Free|Open|Net)BSD, MacOSX/Darwin, and others. Use wget command to grab the source code:
$ wget ftp://ftp.thp.uni-duisburg.de/pub/source/pstree.tar.gz
Use tar command to extract files and build the source code with c compiler (cc):
$ tar zxvf pstree.tar.gz
$ cc -O -o pstree pstree.c
## install binary in /usr/local/bin/ directory ##
$ sudo cp pstree /usr/local/bin

Sample outputs:

Animated gif 01: Installing pstree on Unix operating systems

Animated gif 01: Installing pstree on Unix operating systems

pstree command examples

To see a tree diagram of all the processes currently on the server, type:
# pstree
# pstree | less
# pstree | more

Sample outputs:

Fig.01: pstree command in action

Fig.01: pstree command in action


At the top of the diagram, you will the process init which is the first process that is started when Linux comes on line. init is the parent of all processes on the system, it is executed by the kernel and is responsible for starting all other processes. You can use ASCII characters to draw the tree by passing the -A option:
# pstree -A

Display command line arguments

To see the list of command line arguments, pass the -a option:
$ pstree -a
Sample outputs:

init
  ├─abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messages
  ├─abrtd
  ├─acpid
  ├─atd
  ├─auditd
  │   ├─audispd
  │   │   ├─sedispatch
  │   │   └─{audispd}
  │   └─{auditd}
  ├─crond
  ├─dbus-daemon --system
  │   └─{dbus-daemon}
  ├─hald
  │   ├─hald-runner
  │   │   ├─hald-addon-acpi
  │   │   └─hald-addon-inpu
  │   └─{hald}
  ├─irqbalance --pid=/var/run/irqbalance.pid
  ├─keepalived -D
  │   ├─keepalived -D
  │   └─keepalived -D
  ├─master
  │   ├─pickup -l -t fifo -u
  │   └─qmgr -l -t fifo -u
  ├─mingetty /dev/tty1
  ├─mingetty /dev/tty2
  ├─mingetty /dev/tty3
  ├─mingetty /dev/tty4
  ├─mingetty /dev/tty5
  ├─mingetty /dev/tty6
  ├─nginx
  │   ├─nginx
  │   ├─nginx
  │   └─nginx
  ├─ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
  ├─rhnsd
  ├─rhsmcertd
  ├─rsyslogd -i /var/run/syslogd.pid -c 5
  │   ├─{rsyslogd}
  │   ├─{rsyslogd}
  │   └─{rsyslogd}
  ├─sshd
  │   └─sshd
  │       └─bash
  │           └─pstree -a
  ├─udevd -d
  │   ├─udevd -d
  │   └─udevd -d
  └─vnstatd -d

Display PIDs

To show PIDS for each process name, pass the -p option:
$ pstree -p
Sample outputs:

init(1)-+-NetworkManager(1300)---{NetworkManager}(1313)
        |-accounts-daemon(2448)---{accounts-daemon}(2453)
        |-acpid(1460)
        |-aptd(6958)
        |-atd(1463)
        |-atop(3941)
        |-avahi-daemon(1359)---avahi-daemon(1361)
        |-bamfdaemon(6531)-+-{bamfdaemon}(6532)
        |                  `-{bamfdaemon}(6533)
        |-bluetoothd(1293)
        |-colord(2875)-+-{colord}(2884)
        |              `-{colord}(3091)
        |-console-kit-dae(2493)-+-{console-kit-dae}(2496)
        |                       |-{console-kit-dae}(2497)
.....
...
....
        ├─vmware-vmblock-(1792)─┬─{vmware-vmblock-}(1793)
        │                       └─{vmware-vmblock-}(1794)
        ├─whoopsie(1477)───{whoopsie}(1609)
        ├─zeitgeist-daemo(6740)───{zeitgeist-daemo}(6743)
        ├─zeitgeist-datah(6750)───{zeitgeist-datah}(6754)
        └─zeitgeist-fts(6748)─┬─cat(6756)
                              └─{zeitgeist-fts}(6755)

How do I sort processes?

To sort processes with the same ancestor by PID instead of by name i.e. numeric sort, pass the -n options as follows:
$ pstree -n
$ pstree -np

How can I see who is the owner/user of a process?

To find out the owner of a process in parenthesis, pass the -u option to pstree command:
$ pstree -u
Sample outputs:

init─┬─abrt-dump-oops
     ├─abrtd
     ├─acpid
     ├─atd
     ├─auditd─┬─audispd─┬─sedispatch
     │        │         └─{audispd}
     │        └─{auditd}
     ├─crond
     ├─dbus-daemon(dbus)───{dbus-daemon}
     ├─hald(haldaemon)─┬─hald-runner(root)─┬─hald-addon-acpi(haldaemon)
     │                 │                   └─hald-addon-inpu
     │                 └─{hald}
     ├─irqbalance
     ├─keepalived───2*[keepalived]
     ├─master─┬─pickup(postfix)
     │        └─qmgr(postfix)
     ├─6*[mingetty]
     ├─nginx───3*[nginx(nginx)]
     ├─ntpd(ntp)
     ├─rhnsd
     ├─rhsmcertd
     ├─rsyslogd───3*[{rsyslogd}]
     ├─sshd───sshd───bash───pstree
     ├─udevd───2*[udevd]
     └─vnstatd

How can I highlight the current process and its ancestors?

Pass the -h option to highlight the current process and its ancestors. Pass -H option highlight the specified process (by name/pid):
$ pstree -h
OR highlight process with PID # 60093:
$ pstree -H 60093
Sample outputs:

Fig. 02: Highlighting processes

Fig. 02: Highlighting processes

How can I view a process by PID or user?

The pstree can either accept PID or username as a command line argument. The syntax is as follows to see info about PID 1313
$ pstree 1313
$ pstree -H 1313

In this following example, display only those tree branches (processes) that have been initiated by a user with a username nixcraft:
$ pstree nixcraft

pstree command options

From the pstree(1) command man page:

OptionMeaning
-aShow command line arguments.
-AUse ASCII line drawing characters.
-cDon't compact identical subtrees.
-hHighlight current process and its ancestors.
-HHighlight this process and its ancestors.
-GUse VT100 line drawing characters.
-lDon't truncate long lines.
-nSort output by PID.
-pShow PIDs; implies -c.
-uShow uid transitions.
-UUse UTF-8 (Unicode) line drawing characters.
-VDisplay version information.
-ZShow SELinux security contexts.

Related media

This tutorials is also available in a quick video format:

See also
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdighostip
Processes Managementbgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searchingwhereiswhich
User Informationgroupsidlastlastcommlognameuserswwhowhoamilidmembers

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 3 comments… read them below or add one }

1 Sri February 6, 2014 at 9:23 am

Hey all

PDF file is not displaying the full info provided on this page. Does anyone have the same problem?

Reply

2 Nix Craft February 6, 2014 at 9:33 am

Try again. All pdf files are cached on server to avoid load issues. The latest version will be automatically released after some time.

HTH.

Reply

3 Mladen February 6, 2014 at 10:36 am

Great article, useful command.

Reply

Leave a Comment

Tagged as: ,

Previous Faq:

Next Faq: