Sponsored Content
Full Discussion: [Tip] ptree for Linux
Top Forums UNIX for Advanced & Expert Users [Tip] ptree for Linux Post 303043001 by MadeInGermany on Wednesday 15th of January 2020 02:51:15 PM
Old 01-15-2020
[Tip] ptree for Linux

Unix (and Linux) uses a process tree that gives a natural security, by simple inheritance of attributes.

The following ptree script shows it. It runs on all Linux flavors.
Mostly useful for debugging.

Code:
#!/bin/sh
# Solaris style ptree

[ -x /usr/bin/ptree ] && exec /usr/bin/ptree "$@"

PATH=/bin:/usr/bin:/usr/sbin:/sbin
export PATH

case $1 in
-*)
 echo "
Usage: ptree [ PID | USER ]
Print process tree
 PID : extract branch for this process
 USER : filter for this (existing) user
 USER PID : do both
"
 exit
;;
*[!0-9]*)
 psopt="-u $1"
 shift
;;
*)
 psopt="-e"
esac
psopt="$psopt -H -o pid= -o args="

if [ -z "$1" ]; then
 ps $psopt
 exit
fi

#some effort to add less to the ps list
tmp=/tmp/ptree.$$
trap 'rm -f $tmp' 0 HUP INT TERM
ps $psopt >$tmp
<$tmp awk '
{ ci=index(substr($0,6),$2); o[ci]=$0 }
ci>s[a] { s[++a]=ci }
$1==pid {
 for(i=1;i<=a;i++) {
  si=s[i]; if(si<=ci) print o[si]
 }
 walkdown=ci
 next
}
ci<=walkdown { exit }
walkdown!=0 { print }
' pid="$1"

Without argument it displays the full process tree, like the pstree command.
When there is a PID argument (number), it shows the part of the process tree with the PID - that is the PID's ancestors and descendants.
Also there can be a USER argument, then it shows the USER's processes from the tree.

A quick exercise: where is my current shell from?
Code:
ptree $$


Last edited by MadeInGermany; 01-16-2020 at 04:40 AM..
These 5 Users Gave Thanks to MadeInGermany For This Post:
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Meaning of ps -afe ,ps -fp and ptree

Hello, I was searching for the meaning of commands like ps -afe ,ps -fp and ptree but was not able to find there exact meaning .... Pls help me in getting these... Thanks in advance, Pradeep (1 Reply)
Discussion started by: PradeepRed
1 Replies

2. Gentoo

ptree for linux

at work, I'm a UNIX administrator, but at home I use openSUSE 11. One of the commands that I use to assist me in trouble shooting A LOT is called ptree process tree. does anyone know of a ptree for linux? yes, I can just use ps -ef and see who the parent pid is and then 'ps -ef | grep <parent... (4 Replies)
Discussion started by: james.witte
4 Replies

3. Red Hat

pfiles and pstack and ptree

Can someone tell me the Linux equivalent for pstack and pfiles and ptree which are Solaris commands. (1 Reply)
Discussion started by: bdsffl
1 Replies

4. Solaris

kill the processes seen under ptree

Hi, How to kill the processes running under ptree ? I am noticing lot of processes running under ptree with ssh ? I tried to kill with -9 option which is not working ? Thanks, Radhika. (2 Replies)
Discussion started by: radhirk
2 Replies

5. Shell Programming and Scripting

prstat from ptree ...

Hi, I know how to figure out the list of PID from my application name : ptree `pgrep MyApp` | awk '{print $1}' But I dont know how to pipe it for prstat -p <pidlist> ptree `pgrep MyApp` | awk '{print $1}' | prstat -p ??? I would like to monitor every ptree PID from my application. ... (4 Replies)
Discussion started by: RickTrader
4 Replies

6. Linux

ct, cu, tip, on commands in Linux

Please help me to find out the equivalent commands for the following solaris commands in Linux 1. ct 2. cu 3. tip 4. on Your help will be appreciated. Thanks in advance. ~Saravana (0 Replies)
Discussion started by: tsaravanan
0 Replies

7. UNIX for Dummies Questions & Answers

Linux Tip: How to Configure PPP for dialup for a single machine?

I shall explain how to setup PPP on your Linux machine, so that you can connect to the Internet using an external modem and a Dialup connection. This article explains the setup for the superuser alone (Not normal users).My Setup : My external modem (56kbps) is connected to the Serial Port :... (0 Replies)
Discussion started by: EliteHussar
0 Replies

8. Linux

similar command of ptree in linux

Hello guys, Is there any command to check the all child processes of a process like `ptree`? ptree is not working in Linux.. Regards, Raghu (3 Replies)
Discussion started by: raghu.iv85
3 Replies
All times are GMT -4. The time now is 03:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy