File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+ # Program name: pstree2
3+ # Usage: bash $0 <pid>
4+ # Description: show the cmdline of the recursively by its ppid,
5+ # like the `pstree` display a tree of processes but only show related items.
6+ # Author: dgden
7+ # Create Date: 2021/5/10
8+ # Create Time: 22:24
9+
10+
11+ # if [ "$(id -u)" != "0" ]; then
12+ # echo "WARNING: This script should be run as root" 2>&1
13+ # fi
14+
15+ [[ " $1 " == " " ]] && echo " usage: show the ppid recursively of the pid, using with \" $0 <pid>\" " && exit 1
16+
17+ current_pid=" $1 "
18+
19+ # shellcheck disable=SC2001
20+ current_pid_grep=$( echo " $current_pid " | sed ' s/^\(.\)/[\1]/' )
21+
22+ # shellcheck disable=SC2009
23+ ps -ef | grep " $current_pid_grep " | awk -v pid=" $current_pid " ' $2==pid' | grep " $current_pid_grep "
24+
25+ # shellcheck disable=SC2009
26+ current_ppid=" $( ps -ef | grep " $current_pid_grep " | awk -v ppid=" $current_pid " ' $2==ppid {print $3}' ) "
27+
28+ if [[ " $current_ppid " == " " || " $current_ppid " == " 0" ]]; then
29+ echo " the pid not exists."
30+ exit 1
31+ fi
32+
33+ while [[ $current_ppid -ne 1 ]]; do
34+ # shellcheck disable=SC2001
35+ current_ppid_grep=$( echo " $current_ppid " | sed ' s/^\(.\)/[\1]/' )
36+ # shellcheck disable=SC2009
37+ ps -ef | grep " $current_ppid_grep " | awk -v pid=" $current_ppid " ' $2==pid'
38+
39+ # shellcheck disable=SC2009
40+ current_ppid=" $( ps -ef | grep " $current_ppid_grep " | awk -v ppid=" $current_ppid " ' $2==ppid {print $3}' ) "
41+ if [[ $current_ppid -eq 1 ]]; then
42+ break
43+ fi
44+ done
You can’t perform that action at this time.
0 commit comments