例68
需求:给出一个进程的PID,打印该进程下的子进程及子进程下的所有子进程
#!/bin/bash
#这个脚本用来打印子进程
#作者:xzm
#日期:2019-12-16
ps -elf >/tmp/pid.txt
read -p "please input a pid: " p
if [ -z "$p" ]
then
echo "你没有输入pid"
exit
fi
if ! grep -qw "$p" /tmp/pid.txt
then
echo "你输入的PID不存在."
exit
fi
get_cpid()
{
p1=$1
ps -elf | awk -v p2=$p1 '$5==p2 {print $4}' >/tmp/$p1.txt
n=`wc -l /tmp/$p1.txt|awk '{print $1}'`
if [ $n -eq 0 ]
then
echo "进程$1下没有子进程"
else
echo "进程$1下的子进程时:"
cat /tmp/$p1.txt
fi
}
get_cpid $p
for c_p in `cat /tmp/$p.txt`
do
get_cpid $c_p
done