Feat: Return a PID in edge cases
- Return the first ancestor PID if root_name is not found. - Return the process' own PID if its own name is root_name.
This commit is contained in:
parent
2ee11a5eac
commit
1bf05d0972
28
rpid
28
rpid
@ -21,6 +21,16 @@ EXAMPLES:
|
|||||||
process.
|
process.
|
||||||
|
|
||||||
rpid $$
|
rpid $$
|
||||||
|
|
||||||
|
Get the PID of the first ancestor of the current process, if there
|
||||||
|
is no ancestor named "not_a_process."
|
||||||
|
|
||||||
|
rpid $$ not_a_process
|
||||||
|
|
||||||
|
Get the PID of the current process itself, if it is named
|
||||||
|
"current_process."
|
||||||
|
|
||||||
|
rpid $$ current_process
|
||||||
'
|
'
|
||||||
|
|
||||||
# Validate the arguments
|
# Validate the arguments
|
||||||
@ -51,13 +61,19 @@ get_info() {
|
|||||||
ppid="${ppid%%[[:space:]]}"
|
ppid="${ppid%%[[:space:]]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
next_pid="${pid}"
|
prev_pid="${pid}"
|
||||||
while [[ "${name}" != "${root_name}" && "${ppid}" -ne 1 ]]; do
|
current_pid="${pid}"
|
||||||
get_info "${next_pid}";
|
while [[ "${name}" != "${root_name}" && -r "/proc/${current_pid}/status" ]]; do
|
||||||
name_pid="${next_pid}"
|
mapfile info < \
|
||||||
next_pid="${ppid}"
|
<(grep --null -E -m 2 '^(Name|PPid):' "/proc/${current_pid}/status" \
|
||||||
|
| sort | cut -f 2)
|
||||||
|
name="${info[0]##[[:space:]]}"
|
||||||
|
name="${name%%[[:space:]]}"
|
||||||
|
prev_pid="${current_pid}"
|
||||||
|
current_pid="${info[1]##[[:space:]]}"
|
||||||
|
current_pid="${current_pid%%[[:space:]]}"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
printf '%s\n' "${prev_pid}"
|
||||||
if [[ "${name}" != "${root_name}" ]]; then exit 1; fi
|
if [[ "${name}" != "${root_name}" ]]; then exit 1; fi
|
||||||
printf '%s\n' "${name_pid}"
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user