Artificial intelligent assistant

How can I get only the ancestry processes of a given process? Is it correct that * `pstree <pid>` will output all the descendant processes of the given process * `pstree -s <pid>` will output all the descendant processes and ancestry processes of the given process How can I get only the ancestry processes of a given process? Thanks.

You can always walk the ancestry tree by hand using `ps -o ppid=`:


#! /bin/bash -
pid=${1?Please give a pid}
while
[ "$pid" -gt 0 ] &&
read -r ppid name < <(ps -o ppid= -o comm= -p "$pid")
do
printf '%s\
' "$pid $name"
pid=$ppid
done


Or to avoid running `ps` several times:


#! /bin/sh -
pid=${1?Please give a pid}
ps -Ao pid= -o ppid= -o comm= |
awk -v p="$pid" '
{
pid = $1; ppid[pid] = $2
sub(/([[:space:]]*[[:digit:]]+){2}[[:space:]]*/, "")
name[pid] = $0
}
END {
while (p) {
print p, name[p]
p = ppid[p]
}
}'

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy c84c563f16d8180e47b95476d86b3e47