防止进程使用pkill杀死自己 [英] Prevent process from killing itself using pkill
问题描述
我正在为启动服务脚本编写停止例程:
I'm writing a stop routine for a start-up service script:
do_stop()
{
rm -f $PIDFILE
pkill -f $DAEMON || return 1
return 0
}
问题在于pkill(与killall相同)也与代表脚本本身的进程匹配,并且它基本上会自行终止.该如何解决?
The problem is that pkill (same with killall) also matches the process representing the script itself and it basically terminates itself. How to fix that?
推荐答案
您可以从结果中明确过滤出当前的PID:
You can explicitly filter out the current PID from the results:
kill $(pgrep -f $DAEMON | grep -v ^$$\$)
要正确使用 -f
标志,请确保提供守护程序的整个路径,而不仅仅是子字符串.这样可以防止您杀死脚本(并消除对上面的 grep
的需要),也可以防止杀死恰好共享该守护程序名称的所有其他系统进程.
To correctly use the -f
flag, be sure to supply the whole path to the daemon rather than just a substring. That will prevent you from killing the script (and eliminate the need for the above grep
) and also from killing all other system processes that happen to share the daemon's name.
这篇关于防止进程使用pkill杀死自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!