Initial commit
This commit is contained in:
22
snippets/get_top_level_parent_pid.sh
Executable file
22
snippets/get_top_level_parent_pid.sh
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
function top_level_parent_pid {
|
||||
# Look up the parent of the given PID.
|
||||
pid=${1:-$$}
|
||||
stat=($(</proc/${pid}/stat))
|
||||
ppid=${stat[3]}
|
||||
|
||||
# /sbin/init always has a PID of 1, so if you reach that, the current PID is
|
||||
# the top-level parent. Otherwise, keep looking.
|
||||
if [[ ${ppid} -eq 1 ]] ; then
|
||||
echo ${pid}
|
||||
else
|
||||
top_level_parent_pid ${ppid}
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
echo "Top Level Parent Pid: $(top_level_parent_pid)"
|
||||
echo "Top Level Command: $(ps -o cmd= $(top_level_parent_pid))"
|
||||
|
||||
exit 0
|
||||
Reference in New Issue
Block a user