- $ ps aux
-
PID PPID PGID WINPID TTY UID STIME COMMAND
-
1512 1 1512 3644 con 1003 09:27:17 /usr/bin/bash
-
I 3252 1512 3252 3232 con 1003 09:27:19 /usr/bin/bash
-
4640 1 4640 4640 con 1003 13:12:52 /usr/bin/bash
-
5592 4640 5592 6120 con 1003 15:01:34 /usr/bin/ps
ps命令的格式如上,可根据本机环境调整下面的脚本:
- #!/bin/bash
- root=$1
- root=${root:=1}
- ps aux | awk -F'[ ]+' -vroot=$root '
-
function pad(c, n) {
-
r = ""
-
for(j=0;j
-
if (j < n)
-
r = r " "
-
else {
-
if (j==n) {
-
r = r "|\n" r "|"
-
}
-
r = r c
-
-
}
-
}
-
return r
-
}
-
function findChild(ppid) {
-
depth++
-
for (i in p) {
-
if (p[i]==ppid) {
-
print pad("-", depth) i"--"pc[i]
-
p[i]=""
-
findChild(i)
-
}
-
}
-
depth--
-
-
}
-
NR>1{p[$2]=$3;pc[$2]=$NF}
-
END{
-
depth = 1
- #root = 1
-
if (root in p) print root"--"pc[root]
-
p[root]=""
-
findChild(root)
-
-
}'
- 效果截图: