Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Last active November 23, 2019 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBronosky/b4cb78201ae39745398824a071bfa4d4 to your computer and use it in GitHub Desktop.
Save RichardBronosky/b4cb78201ae39745398824a071bfa4d4 to your computer and use it in GitHub Desktop.
#!/bin/bash -eu
pid_lineage(){
ps -p ${1:-$$} -o pid,ppid,args | tail -n1 | \
(
read pid ppid args
echo -e "$pid\t$args"
[[ $pid -gt 1 ]] && pid_lineage $ppid -
) | (
[[ ${2:-} != '-' ]] && \
column -t -s $'\t' || \
cat
)
}
pid_lineage "$@"
@RichardBronosky
Copy link
Author

This now works for Linux and macOS.

@TRUPaC
Copy link

TRUPaC commented Nov 23, 2019

Hello Richard, I have came up with an endless loop when one of my top parent processes had parent pid set to itself instead of to 1.

To handle I changed:
[[ $pid -gt 1 ]]
to:
[[ $pid -gt 1 && $pid -ne $ppid ]]

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment