简体   繁体   English

如何退出tail -f并恢复脚本

[英]How to exit tail -f and resume script

I have a simple script, that sets up logging, runs tail -f on the log file and then, after I exit tail , performs some cleanup. 我有一个简单的脚本,它设置日志记录,在日志文件上运行tail -f然后,在我退出tail ,执行一些清理。 Basically something like this 基本上是这样的

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

Problem is, exiting tail by Ctrl+C breaks the script execution too, so cleanup is not called. 问题是,通过Ctrl + C退出tail也会破坏脚本执行,因此不会调用清理。 Is there a way to 'properly' exit tail and resume script call? 有没有办法'正确'退出tail并恢复脚本调用? I found a few solutions based on saving PID and killing it by timeout, but that's not what I want here, I may need the monitoring for a few minutes or few hours, so I'd like to have a manual switch. 我找到了一些基于保存PID的解决方案,并通过超时将其终止,但这不是我想要的,我可能需要监控几分钟或几个小时,所以我想要一个手动切换。

You can do something like this 你可以做这样的事情

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM