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)
  • filter process 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.9.tar.gz (22.1 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.9-py2.py3-none-any.whl (23.2 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: pgtree-1.1.9.tar.gz
  • Upload date:
  • Size: 22.1 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.9.tar.gz
Algorithm Hash digest
SHA256 3216044601602c7363deef82c398abce7cc51929aff1ebab94bdf83e0f85a21d
MD5 4a4f093255314b95ba52a5e5df39eab6
BLAKE2b-256 99c257c3a535e0131321418b8647fb23d8d671407094d6ee08363f392f3f348f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pgtree-1.1.9-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.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 04f0249cffceefc3e15360164a9d32dca999bdeeb27156aa923cbf47725c51b3
MD5 fbd0a5073a2253f5804fd648707b1fb2
BLAKE2b-256 27d0c0b3a3390c41e698c7ecc63c19fb8c29890136edb18681bdef38c3e0c2b5

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