[英]bash script to make `tail -f` exit when the file is moved or deleted
[英]How to exit tail -f and resume script
我有一个简单的脚本,它设置日志记录,在日志文件上运行tail -f
然后,在我退出tail
,执行一些清理。 基本上是这样的
echo 'monitoring started'
tail -f /var/log/some.log
echo 'never gets here'
问题是,通过Ctrl + C退出tail
也会破坏脚本执行,因此不会调用清理。 有没有办法'正确'退出tail
并恢复脚本调用? 我找到了一些基于保存PID的解决方案,并通过超时将其终止,但这不是我想要的,我可能需要监控几分钟或几个小时,所以我想要一个手动切换。
你可以做这样的事情
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.