homerzhou 發(fā)表于 2015-10-29 10:11
我覺得出現(xiàn)ctrl + c 無效,并不和ping是不是正在執(zhí)行有關(guān),而是和ping捕捉信號有關(guān),因為如果把ping修改為sleep 2000,其實sleep也是一直在運行,可是這時只要按一次ctrl + c 就能停止test.sh運行,而且我覺得 ctrl + c 是發(fā)給前臺進程組,不是單單一個前臺進程的
Bash 等終端的默認(rèn)行為是這樣的:當(dāng)按下 CTRL-C 之后,它會向當(dāng)前的整個進程組發(fā)出 SIGINT 信號。而 sleep 是由當(dāng)前腳本調(diào)用的,是這個腳本的子進程,默認(rèn)是在同一個進程組的,所以也會收到 SIGINT 并停止執(zhí)行
這篇文檔給了我們一個更準(zhǔn)確的說明——如果當(dāng)前正有一個外部命令在前臺執(zhí)行,那么 trap 會等待當(dāng)前命令結(jié)束以后再處理信號隊列中的信號。
On most operating systems, ping is an example of a misbehaving process. It traps SIGINT in order to display a summary at the end, before exiting. But it fails to kill itself with SIGINT, and so the calling shell does not know that it should abort as well
歡迎光臨 Chinaunix (http://www.72891.cn/) | Powered by Discuz! X3.2 |