Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/joknarf/pgtree

Unix process hierachy tree for specific processes (mixed pgrep + pstree)
https://github.com/joknarf/pgtree

cli command-line hierarchy linux pgrep pkill process pstree sysadmin tool tree unix

Last synced: 3 days ago
JSON representation

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

Awesome Lists containing this project

README

        

[![Codecov](https://codecov.io/github/joknarf/pgtree/coverage.svg?branch=master)](https://codecov.io/gh/joknarf/pgtree)
[![Upload Python Package](https://github.com/joknarf/pgtree/workflows/Upload%20Python%20Package/badge.svg)](https://github.com/joknarf/pgtree/actions?query=workflow%3A%22Upload+Python+Package%22)
[![Pypi version](https://img.shields.io/pypi/v/pgtree.svg)](https://pypi.org/project/pgtree/)
[![Downloads](https://pepy.tech/badge/pgtree)](https://pepy.tech/project/pgtree)
[![Python versions](https://img.shields.io/badge/python-2.3+%20|%203.x-blue.svg)](https://shields.io/)
[![Licence](https://img.shields.io/badge/licence-MIT-blue.svg)](https://shields.io/)

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

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

`-T` option to display threads only works if `ps ax -T -o spid,ppid` 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)_

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

installation using pip:
```
# pip install pgtree
```

## Usage
```
# pgtree -h
usage: pgtree.py [-W] [-RIya] [-C ] [-O ] [-c|-k|-K] [-1|-p ,...|]

-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,...] : display multiple instead of 'stime' in output
must be valid with ps -o command

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

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

found pids are prefixed with ▶
```
## 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](https://user-images.githubusercontent.com/10117818/215317322-7df4559c-ccf4-41f6-b008-55d1fc8f0bb7.png)

## Demo

output