Skip to main content
 首页 » 编程设计

linux之退出 shell 脚本时如何杀死所有子进程

2023年11月22日115freeliver54

我有一个运行程序的 C shell 脚本。该程序产生了一个 child 。当我通过 ctrl-C 将 SIGINT 发送到我的 shell 脚本时,shell 脚本以及我认为它产生的进程退出。但是,最后一个过程仍然存在。我如何指示 C shell 在退出之前杀死所有子进程?

请您参考如下方法:

如何为 SIGINT 创建一个信号陷阱? See the Interrupt Handling section here :

#!/bin/csh 
 
# Setup sigint handler 
onintr close 
 
# start a background process 
sleep 1000& 
setenv pid $! 
 
while (1 == 1) 
  echo Program is running 
  sleep 2 
end 
 
close: 
echo End of program. Kill $pid 
kill $pid