思路: ps -elf查看进程ppid根据ppid赛选pid进程
#!/bin/bash
#The script is used to print child processes.
#Date 2021-01-16
file=/tmp/pid.txt #定义文本路径
while :
do
read -p "please input a pid: " pid
if [ -z $pid ];then#判断是否为空
echo "The input can not be empty (input q or quit to exit)."
continue
elif [ $pid == "q" ]||[ $pid == "quit" ];then#退出循环
echo "exit."
exit
fi
ps -elf| awk -v pid2=$pid '$5==pid2 {print $0}'>$file#筛选pid
if ! grep -qw "$pid" $file;then#过滤pid
echo "The input pid does not exist (input q or quit to exit)."
continue
fi
break
done
child_pid() {
ps -elf|awk -v pid_new=$1 '$5==pid_new {print $4}' >$1.txt
#将进程id写入文本
n=`wc -l $1.txt| awk '{print $1}'`
if [ $n -eq 0 ];then
echo "There are no subprocess under the pid $1"
elif [ $n -ne 0 ];then
echo "The pid $1 subprocess according to following:"
cat $1.txt
fi
}#定义筛选函数
child_pid $pid
for i in `cat $pid.txt`
#for循环将每个子进程包含的子进程筛选出
do
child_pid $i
done
for i in `cat $pid.txt`
#for循环遍历删除文本
do
[ -f $i.txt ] && rm $i.txt
done
[ -f $pid.txt ] && rm $pid.txt#删除文本
68.查看子进程脚本
原创Margotchen 博主文章分类:shell应用100 ©著作权
©著作权归作者所有:来自51CTO博客作者Margotchen的原创作品,谢绝转载,否则将追究法律责任
上一篇:50.检测网卡状态脚本
下一篇:69.批量创建mysql用户
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章