2016-02-08 22 views
1

我有一個簡單的腳本,設置日誌記錄,在日誌文件上運行tail -f,然後在我退出tail後執行一些清理。基本上是這樣的如何退出尾部-f和恢復腳本

echo 'monitoring started' 
tail -f /var/log/some.log 
echo 'never gets here' 

問題是,按Ctrl + C退出tail也打破了腳本執行,因此清理不叫。有沒有辦法'正常'退出tail並恢復腳本調用?我發現了一些解決方案,它們基於保存PID並在超時後將其終止,但這不是我想要的,我可能需要監控幾分鐘或幾個小時,所以我想要手動切換。

回答

3

你可以做一些這

echo "monitoring started" 
tail -f /var/log/some.log & #execute tail in background 
read -sn 1 #wait for user input 
kill %1 #kill the first background progress, i.e. tail 
echo "Is reached" 
+0

感謝狀,這完美的作品。 –