
有 PowerShell 腳本,我可以從中執行以下命令:putty.exe -ssh user@srv -pw password -m Execute_Command_File -t
在腳本執行過程中,tailf /dir/log/
命令被寫入到 Execute_Command_File 中。執行腳本後,請求的會話將會開啟並tailf
正在執行。
問題是,當我嘗試退出tailf
(ctrl+C)時,它會關閉終端。
試圖/bin/bash
在 的末尾添加Execute_Command_File
,但沒有幫助。當然也試過了tail -f/F
,還是不行…
有任何想法嗎?
答案1
它恰好tail
因為 CTRL+C 而死亡,但它也被發送(SINGINT)到父級,bash
。由於預設情況下 bash 在收到此類訊號時會終止,因此您必須替換bash
收到訊號時的預設行為。
使用陷阱內建指令 可以bash(1)
更改此設定。
以下腳本tailf-ctrl.sh
是一個演示並顯示了答案:
#!/bin/bash
function finish {
echo "CTRL-C pressed!"
}
F=testfile
echo hello > $F
# set custom action
trap finish SIGINT # comment this to see the problem
tail -f $F
# reset default action
trap - SIGINT
echo "Hello after" > after
cat after
注意:
- 訊號情報是與CTRL+C相關的訊號
- 第一的陷阱安裝與 SIGINT 訊號相關的自訂操作
- 第二陷阱重設 SIGINT 訊號的預設行為
腳本的輸出是:
$ bash tailf-ctrl.sh
hello
^CCTRL-C pressed!
Hello after
這表示第二個檔案已寫入,因此當tail
由於CTRL-C
.
如果你註解掉第一個 trap 指令,你會看到你的問題出現:bash 立即終止,輸出應該是:
$ bash tailf-ctrl.sh
hello
^C
$