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$
source share