Skip to main content

Unix process tree search

Project description

Joknarf Tools Codecov Upload Python Package Pypi version Python versions Licence Downloads Packages

pgtree

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

Features

  • display process tree (parent + children) and optionnally threads
  • filter process/threads tree using pgrep options / pattern
  • limit tree to processes and their children
  • customize ps fields to output
  • can send kill or kill -9 to processes and their children
  • watch process tree
  • supports all unix/linux/macos

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 ax -o pid,ppid,stime,user,ucomm,args

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

-T option to display threads only works if ps ax -T -o spid,ppid / pgrep -w available on system (ubuntu/redhat...)

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

(uses -o fname on Solaris)

Examples

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:

image

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

image

Demo

pgtree

Install

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

install using pip:

# pip install pgtree

or use your prefered method according to your OS:

brew install joknarf/tools/pgtree
sudo dnf install https://github.com/joknarf/pgtree/releases/latest/download/pgtree.rpm
curl -OL https://github.com/joknarf/pgtree/releases/latest/download/pgtree.deb
sudo dpkg -i pgtree.deb
curl -OL https://github.com/joknarf/pgtree/releases/latest/download/pgtree.apk
sudo apk add --allow-untrusted pgtree.apk
curl -OL https://github.com/joknarf/pgtree/releases/latest/download/pgtree.pkg
sudo installer -pkg pgtree.pkg -target /

Usage

# 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
    -T : display threads (ps -T)
    -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 ▶

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgtree-1.1.10.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgtree-1.1.10-py2.py3-none-any.whl (23.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pgtree-1.1.10.tar.gz.

File metadata

  • Download URL: pgtree-1.1.10.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for pgtree-1.1.10.tar.gz
Algorithm Hash digest
SHA256 43db0c95eae4f821735d1a38b5189a4b1f1e2996c761cea5a4537dfc4b0edbc9
MD5 41bb27ab9c25457d6aec292361cf64a1
BLAKE2b-256 8e144a4acc15ad62553c42b3516cb210b2c8c648cc5ae0c123ce81515d0b9eb0

See more details on using hashes here.

File details

Details for the file pgtree-1.1.10-py2.py3-none-any.whl.

File metadata

  • Download URL: pgtree-1.1.10-py2.py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for pgtree-1.1.10-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ffcc5ea4d69322a4b8896a250da25e992c270844550aaeb0a3dfbe6d270cde53
MD5 a9fcae031398fcf6c749dea9ab0149d9
BLAKE2b-256 043f5820795bef673513a4b1f6a9410c26a1fb300e0d382f60c62f08c92e7209

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page