How to display the current process tree of a bash session?

I would like to create a bash alias that gives me the process tree from the current bash session that I am using, prior to init.

A use case is to know if I used the bash or vi :shell command.

I use MacOS X. I heard about pstree , but it seems to only show children, not the connection between init and the current process.

+6
source share
5 answers

I'm sure with a little google search, you can find how to get and download pstree for Mac. However, you can make a poor man version using ps and ppid .

eg,

 ps -eo ppid,pid,cmd | awk '{p[$1]=p[$1]","$3}END{ for(i in p) print i, p[i]}' 
+5
source

This is supported in pstree(1) , using the option to display the tree only for a specific PID code and provide the current PID of the process ( $$ in Bash). The option is named differently between the licensed version of the GPL. Werner Almesberger distributed with Debian and BSD the version of Fred Hacht distributed with MacOS.

  • On Debian / Ubuntu: pstree -s $$

     init───gnome-terminal───bash───pstree 

    Summary of the -s :

     -s Show parent processes of the specified process. 
  • On MacOS: pstree -p $$

     -+= 00001 root /sbin/launchd \-+= 25706philipbranning/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal \-+= 25716 root login -pfl philipbranning /bin/bash -c exec -la bash /bin/bash \-+= 25722 philipbranning -bash \-+= 32456 philipbranning pstree -p 25722 \--- 32457 root ps -axwwo user,pid,ppid,pgid,command 

    Summary of the -p option:

     -p pid show only branches containing process <pid> 

Here is your alias for MacOS:

 alias psme='pstree -p $$' 
+3
source

I used Mac OS 10.7 Lion, but I think it will be quite portable for Bourne-like shells on other Unix-like systems. You may have problems with the command keyword in the ps argument.

I put the following code in a file called procsup.sh, which defines a shell function to keep track of the process’s parents prior to process ID 1. (I often find shell functions to work easier than aliases.)

 procsup() { leaf=$$ ps -eo pid,ppid,command | awk -v leaf="$leaf" \ '{parent[$1]=$2;command[$1]=$3;} function print_ancestry(pid) { print pid " (" command[pid] ") child of " parent[pid]; if(pid!=1) print_ancestry(parent[pid]) }; END{\ print_ancestry(leaf) }' } 

Then I launched the shell and processed procsup.sh. In real life, you need to make sure that your new shells will automatically start procsup.sh at startup, perhaps in your personal .bashrc. First, I checked the pedigree from this shell. Then I started using vi from this shell. As usual, the interaction with vi did not reach the transcript until I did :shell . My terminal window looked like this:

 Mariel:~/Library/Scripts 1j david$ Mariel:~/Library/Scripts 1j david$ Mariel:~/Library/Scripts 1j david$ . procsup.sh Mariel:~/Library/Scripts 1j david$ procsup 41926 (-bash) child of 41922 41922 (login) child of 41917 41917 (/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal) child of 19281 19281 (/sbin/launchd) child of 1 1 (/sbin/launchd) child of 0 Mariel:~/Library/Scripts 1j david$ Mariel:~/Library/Scripts 1j david$ Mariel:~/Library/Scripts 1j david$ vi bash-3.2$ # Have just done :shell. bash-3.2$ . procsup.sh bash-3.2$ procsup 42325 (/bin/bash) child of 42324 42324 (vi) child of 41926 41926 (-bash) child of 41922 41922 (login) child of 41917 41917 (/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal) child of 19281 19281 (/sbin/launchd) child of 1 1 (/sbin/launchd) child of 0 bash-3.2$ bash-3.2$ 
+2
source

If you are using a package manager such as MacPorts, you can easily install pstree.

+1
source

I don’t have all the answer you are looking for, but I have an idea that can lead you in the right direction. Command

 declare -A parent 

will create an associative array (hash if you speak Perl)

You will need some command that will give you name-value pairs for PID and PPID ... I assume that the mac ps command can be done to do this if you mix it up enough. I will use "ps -eo" as above, but you will want to fill in the blanks.

Then you can do something like this:

 ps -eo pid,ppid | while read pid ppid do parent[$pid]=$ppid echo "pid: $pid ppid: ${parent[$pid]} grandppid: ${parent[${parent[$pid]}]}" done 

I am having trouble keeping $ parent values ​​outside of my while loop, otherwise I would create a second loop to go from $$ back to init. I will leave this as an exercise for the reader.

+1
source

Source: https://habr.com/ru/post/895531/


All Articles