Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

vi

process

ps

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

The use case is to know whether I have used bash or vi's :shell command.

I am using MacOS X. I have heard about pstree, but it seems to only show children, not the relationship between init and the current process.

like image 825
Thaddee Tyl Avatar asked Aug 21 '11 14:08

Thaddee Tyl


People also ask

What command displays all processes in a tree format?

pstree is a Linux command that shows the running processes as a tree. It is used as a more visual alternative to the ps command. The root of the tree is either init or the process with the given pid.

How can I see active processes in Linux?

Open the terminal window on Linux. For remote Linux server use the ssh command for log in purpose. Type the ps aux to see all running process in Linux. Alternatively, you can issue the top command or htop command to view running process in Linux.


2 Answers

I am sure with a a bit of google search, you can find how to get and download pstree for the Mac. However, you can do a poor man's 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]}'
like image 76
ghostdog74 Avatar answered Oct 22 '22 11:10

ghostdog74


This is supported in pstree(1) by using an option to show the tree only for a particular PID and providing the current process's PID ($$ in Bash), The option is named differently between GPL-licensed version by Werner Almesberger distributed with Debian and the BSD version by Fred Hucht distributed with MacOS.

  • On Debian/Ubuntu: pstree -s $$

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

    Summary of -s option:

    -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 -p option:

    -p pid    show only branches containing process <pid>
    

Here's your alias for MacOS:

alias psme='pstree -p $$'
like image 21
pneumatics Avatar answered Oct 22 '22 11:10

pneumatics