Blog

03/06/2019

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



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.[donotprint]

pstree command details
DescriptionSee running processes in the
form of a tree diagram
Category
Difficulty
Root privilegesNo
Estimated completion time10m
[/donotprint] 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

Install pstree on MacOS X

Type the following command:
$ brew install pstree

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

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:

Fig.02: Display running processes along with command line arguments

Display PIDs

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

systemd(1)-+-accounts-daemon(2873)-+-{gdbus}(3022)
           |                       `-{gmain}(2984)
           |-acpid(2845)
           |-apt.systemd.dai(22456)---unattended-upgr(23394)---http(23407)
           |-atd(2881)
           |-atop(27818)
           |-cgmanager(2876)
           |-cron(2841)
           |-dbus-daemon(2892)
           |-dnsmasq(3289)
           |-dnsmasq(5693)---dnsmasq(5694)
           |-haveged(744)
           |-irqbalance(3179)
           |-iscsid(3057)
           |-iscsid(3065)
           |-libvirtd(20176)-+-{libvirtd}(20177)
           |                 |-{libvirtd}(20178)
           |                 |-{libvirtd}(20179)
           |                 |-{libvirtd}(20180)
           |                 |-{libvirtd}(20181)
           |                 |-{libvirtd}(20182)
           |                 |-{libvirtd}(20183)
           |                 |-{libvirtd}(20184)
           |                 |-{libvirtd}(20185)
           |                 |-{libvirtd}(20186)
           |                 |-{libvirtd}(20187)
           |                 |-{libvirtd}(20188)
           |                 |-{libvirtd}(20189)
           |                 |-{libvirtd}(20190)
           |                 `-{libvirtd}(20191)
           |-login(3180)---bash(6855)
           |-lvmetad(633)
           |-lxcfs(2837)-+-{lxcfs}(2973)

.....
...
....
           |-sshd(3028)---sshd(23065)---sshd(23161)---bash(23162)---pstree(25110)
           |-sudo(11906)---corefreqd-mgr(11907)-+-{corefreqd-kill}(11908)
           |                                    |-{corefreqd/0}(11909)
           |                                    |-{corefreqd/1}(11910)
           |                                    |-{corefreqd/2}(11911)
           |                                    |-{corefreqd/3}(11912)
           |                                    |-{corefreqd/4}(11913)
           |                                    |-{corefreqd/5}(11914)
           |                                    |-{corefreqd/6}(11915)
           |                                    `-{corefreqd/7}(11916)
           |-systemd(6845)---(sd-pam)(6850)
           |-systemd-journal(745)
           |-systemd-logind(2950)
           |-systemd-timesyn(2689)---{sd-resolve}(2726)
           |-systemd-udevd(667)
           |-tclsh(3144)---{tclsh}(6199)
           |-virtlockd(20121)---{virtlockd}(20124)
           |-virtlogd(5910)---{virtlogd}(19998)
           `-zed(2848)

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:

systemd-+-accounts-daemon-+-{gdbus}
        |                 `-{gmain}
        |-acpid
        |-apt.systemd.dai---unattended-upgr---http(_apt)
        |-atd(daemon)
        |-atop
        |-cgmanager
        |-cron
        |-dbus-daemon(messagebus)
        |-dnsmasq(lxd)
        |-dnsmasq(libvirt-dnsmasq)---dnsmasq(root)
        |-haveged
        |-irqbalance
        |-2*[iscsid]
        |-libvirtd---15*[{libvirtd}]
        |-login---bash(vivek)
        |-lvmetad
        |-lxcfs---3*[{lxcfs}]
        |-lxd---22*[{lxd}]
        |-lxd---systemd(100000)-+-accounts-daemon-+-{gdbus}
        |                       |                 `-{gmain}
        |                       |-agetty
        |                       |-atd(100001)
        |                       |-cron
        |                       |-dbus-daemon(100107)
        |                       |-dhclient
        |                       |-lighttpd(100033)
        |                       |-memcached(100113)---5*[{memcached}]


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. 03: 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
Package Managerapkapt
Processes Managementbgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searchingwhereiswhich
User Informationidgroupslastlastcommlognameuserswwhowhoamilid/libuser-lidmembers

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...