我有以下 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
关于java - 如果通过 CTRL + C 终止脚本,如何终止由脚本启动的 Java 进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11184475/