退出shell脚本时如何终止所有子进程?

时间:2022-02-21 00:37:08

I've got a C shell script that runs a program. That program spawns off a child. When I send a SIGINT to my shell script via ctrl-C, the shell script exits as well as the process that it spawned I think. However, the last process remains. How can I instruct C shell to kill all child processes before it exits then?

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

1 个解决方案

#1


1  

How about creating a signal trap for SIGINT? See the Interrupt Handling section here:

如何为SIGINT创建信号陷阱?请参阅此处的“中断处理”部分:

#!/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

#1


1  

How about creating a signal trap for SIGINT? See the Interrupt Handling section here:

如何为SIGINT创建信号陷阱?请参阅此处的“中断处理”部分:

#!/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