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.
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.
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.
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]}'
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 $$'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With