![](/img/trans.png)
[英]bash how to kill parent process, or exit from parent process from a function in bash module script
[英]Bash : Find parent process id of a script from another script and take some actions
我有一个脚本workingScript.sh。我有另一个脚本,称为monitorscript.sh,我想用它来监控workingScript.sh 的processid 活动。
我想确保如果除了从 17506(workingScript.sh 的父进程)生成的那些之外创建了任何wc/cat进程 id,它应该被 monitorscript.sh 杀死
ps aux|grep "workingScript*"
svc.test+ 25896 0.0 0.0 112812 984 pts/3 S+ 01:15 0:00 grep --color=auto workingScript*
svc.test+ 17506 0.0 0.0 113288 1072 ? S Mar18 0:00 sh workingScript.sh
svc.test+ 17510 0.0 0.0 113412 868 ? S Mar18 0:03 sh workingScript.sh
workingScript.sh 父进程 ID 的进程树显示如下。
pstree -p 17506
sh(17506)─┬─cat(17509)
└─sh(17510)─wc(17569)
因此,在任何时候,如果任何 wc/cat 进程 id 除了由 workingScript.sh 创建的进程 id (在本例中为 17509/17569 根据上述进程树)之外,它都应该被杀死
我们可以使用PPID来获取相同脚本的进程 ID,但是我们如何获取不同脚本的不同脚本和不同脚本的相关子进程
有人可以帮帮我吗?
使用LD_PRELOAD
和覆盖的exec*()
调用运行workingScript.sh
。 然后在这些exec*()
调用中检查命令是否解析为wc
或cat
可执行文件。 如果是的话,如果你想raise
一些杀戮信号。 请记住导出LD_PRELOAD
以便子进程exec*()
也被覆盖。
如果您希望定期检查脚本中的进程,则只需找到 pid 并杀死它们:
pid=$(pgrep workingScript.sh)
while sleep "$polling_period"; then
# ex. killing cat
pkill -P "$pid" cat
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.