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)
You can now take a look at joknarf/pgtreeawk, a pgtree implementation using bash/awk (more portable, no python needed)
And also a pure bash implementation (but a little slower) joknarf/pgt

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.13.tar.gz (22.6 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.13-py2.py3-none-any.whl (24.0 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: pgtree-1.1.13.tar.gz
  • Upload date:
  • Size: 22.6 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.13.tar.gz
Algorithm Hash digest
SHA256 b7ac72f159cb5131312d13c6219369e5f2c398b41a6ec91330084e773a061f8d
MD5 16bfb22268e393aa826548262166f05f
BLAKE2b-256 d98f828e860365b5216a5258a1bc7b58501cde2127fdb187cd80f78846e42830

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pgtree-1.1.13-py2.py3-none-any.whl
  • Upload date:
  • Size: 24.0 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.13-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6edfd31c2d4f0139c4408ee30b4681a7d84aad068aa1a254f5683526cab5d3dc
MD5 81f3d0175c8dacc5fcebea0ab0c48ae9
BLAKE2b-256 0dcd06d2b43d47f51f21262944f87ecd4aed204f1c8203eac689abf6afb06c28

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