Unix process tree search

shell, pstree, pgrep, process, tree, cli, command-line, hierarchy, linux, pkill, sysadmin, tool, unix
pip install pgtree==1.0.26


Travis CI Codecov Upload Python Package Pypi version Downloads Python versions Licence


Unix process hierachy tree display for specific processes (kind of mixed pgrep + pstree)

pgtree is also able to send signal to found processes and all their children

The purpose is to have the tool working out of the box on any Unix box, using the default OS python installed, without installing anything else. The code must be compatible with python 2.x + 3.x

Should work on any Unix that can execute :

# /usr/bin/pgrep 
# /usr/bin/ps -e -o pid,ppid,stime,user,ucomm,args

if pgrep command not available (AIX), pgtree uses built-in pgrep (-f -i -x -u <user> supported).

Tested on various versions of RedHat / CentOS / Ubuntu / Debian / Suse / MacOS / Solaris / AIX including old versions

(uses -o comm on Solaris)


FYI, the pgtree/pgtree.py is standalone and can be directly copied/used anywhere without any installation.

installation using pip:

# pip install pgtree

installation using setup.py, root install in /usr/local/bin:

# ./setup.py install

installation using setup.py, user install in ~/.local/bin:

# ./setup.py install --prefix=~/.local


# pgtree -h
    usage: pgtree.py [-W] [-RIya] [-C <when>] [-O <psfield>] [-c|-k|-K] [-1|-p <pid1>,...|<pgrep args>]

    -I : use -o uid instead of -o user for ps command
         (if uid/user mapping is broken ps command can be stuck)
    -c : display processes and children only 
    -k : kill -TERM processes and children
    -K : kill -KILL processes and children
    -y : do not ask for confirmation to kill
    -R : force use of internal pgrep
    -C : color preference : y/yes/always or n/no/never (default auto)
    -w : tty wrap text : y/yes or n/no (default y)
    -W : watch and follow process tree every 2s
    -a : use ascii characters
    -O <psfield>[,psfield,...] : display multiple <psfield> instead of 'stime' in output
                   <psfield> must be valid with ps -o <psfield> command

    by default display full process hierarchy (parents + children of selected processes)

    -p <pids> : select processes pids to display hierarchy (default 0)
    -1 : display hierachy children of pid 1 (not including pid 0)
    <pgrep args> : use pgrep to select processes (see pgrep -h)

    found pids are prefixed with ▶


show all parents and children of processes matching bash

# pgtree bash

show processes matching bash and their children

# pgtree -c bash

kill all sh processes of user joknarf and their children

#pgtree -k -u joknarf -x sh

Customize ps output fields:


Put default options in PGTREE env variable:

# export PGTREE='-1 -O %cpu,stime -C y'
# pgtree

Use watch utility to follow process tree:

# pgtree -W bash