我有以下script1.sh
:
#!/bin/bash trap 'echo "Exit signal detected..."; kill %1' 0 1 2 3 15 ./script2.sh & #starts a java app ./script3.sh #starts a different java app
当我做CTRL + C时,它会终止script1.sh
,但由script2.sh
启动的Java Swing应用程序仍然保持打开状态。 它怎么没有杀死它?
我觉得像这样的东西可以为你工作。 但是,@carlspring提到你最好在每个脚本中都有类似的东西,这样你就可以捕获相同的中断并杀死任何丢失的子进程。
采取一切
#!/bin/bash # Store subproccess PIDS PID1="" PID2="" # Call whenever Ctrl-C is invoked exit_signal(){ echo "Sending termination signal to childs" kill -s SIGINT $PID1 $PID2 echo "Childs should be terminated now" exit 2 } trap exit_signal SIGINT # Start proccess and store its PID, so we can kill it latter proccess1 & PID1=$! proccess2 & PID2=$! # Keep this process open so we can close it with Ctrl-C while true; do sleep 1 done
那么,如果你在后台模式下启动脚本(使用&
),那么在调用脚本退出之后,这是正常的行为。 您需要通过将echo $$
存储到文件来获取第二个脚本的进程ID。 然后让相应的脚本有一个stop
命令,当你调用它时会stop
这个过程。