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

Thaddee Tyl picture Thaddee Tyl · Aug 21, 2011 · Viewed 21.3k times · Source

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.

Answer

ghostdog74 picture ghostdog74 · Aug 21, 2011

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]}'