有什麼辦法來調用一個子進程,以便它和它的所有後代發送一箇中斷,就像你Ctrl-C的前臺任務一樣嗎?我試圖殺死一個調用長時間運行的孩子的啓動腳本。我試過kill -SIGINT $child
(它不會將中斷髮送給它的後代,所以是沒有操作的)和kill -SIGINT -$child
(它在交互式調用時工作,但在腳本中運行時不起作用)。bash腳本如何使Ctrl-C等同於後臺任務?
這是一個測試腳本。長時間運行的腳本是test.sh --child
。當你致電test.sh --parent
時,它會調用test.sh --child &
然後嘗試殺死它。我如何讓父母成功地殺死孩子?
#!/bin/bash
if [ "$1" = "--child" ]; then
sleep 1000
elif [ "$1" = "--parent" ]; then
"$0" --child &
for child in $(jobs -p); do
echo kill -SIGINT "-$child" && kill -SIGINT "-$child"
done
wait $(jobs -p)
else
echo "Must be invoked with --child or --parent."
fi
我知道,你可以修改長時間運行的孩子trap
信號,將其發送到其子進程,然後等待(從 Bash script kill background (grand)children on Ctrl+C),但有沒有修改的孩子腳本什麼辦法?
已更新的答案。 – user000001