工作中想要在shell脚本中trap kill信号,实现kill所有子进程的操作,尝试了各种方法,trap了包括但不限于 TERM INT SIGINT SIGTERM SIGKILL 等信号,无一生效,最后在Stack Overflow上找到答案,kill -9的信号不能被trap,否则无法保证操作系统能够kill所有的进程。
大佬的回答:
You cannot do that. Yes 9 is
SIGKILL
and Unix system by design doesn't allow any script/program to trapSIGKILL
due to security reasons. Otherwise any script can trap & ignore SIGKILL which will make impossible to terminate that script by OS.