简体   繁体   English

从shell脚本退出时,如何杀死所有子进程?

[英]How can I kill all child processes when exiting from my shell script?

I've got a C shell script that runs a program. 我有一个运行程序的C shell脚本。 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. 当我通过ctrl-C将SIGINT发送到我的shell脚本时,该shell脚本以及它产生的进程都会退出。 However, the last process remains. 但是,最后一个过程仍然存在。 How can I instruct C shell to kill all child processes before it exits then? 我如何指示C Shell在退出子进程之前将其杀死?

How about creating a signal trap for SIGINT? 如何为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM