January 2012
This program is_ancestor:
#!/bin/bash
have_ancestor()
{
[ -z ${1} ] && echo "have_ancestor needs a name to search" && return 1
PARENT_PID=$$
while ! [ ${PARENT_PID} -eq 0 ] ; do
if ps ho '%c' ${PARENT_PID}|grep -qw "${1}" ; then
return 0
fi
PARENT_PID=$(ps ho '%P' ${PARENT_PID}|tr -d '\t ')
done
return 1
}
if have_ancestor "${@}" ; then
echo "Yes, ${@} is in our ancestry"
else
echo "No, we don't descend from ${@}"
fi
... will tell you whether a process name is the parent of your process, or its parent, etc. all the way up to init, if you call it like this:
apprentice@testbox:~$ is_ancestor sshd
Yes, sshd is in our ancestry