Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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

This comment has been minimized.

Copy link
Owner Author

RichardBronosky commented Nov 4, 2019

This now works for Linux and macOS.

@TRUPaC

This comment has been minimized.

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
You can’t perform that action at this time.