bash – 在shell脚本中启动末尾
我有以下
>写入stdout的Java进程 即使我在代码中出现0,我也不能结束tail -f进程。 哪个不让我的脚本完成。有什么其他的方法可以在Bash吗? 代码如下所示。 function startServer() { touch logfile startJavaprocess > logfile & tail -f logfile | while read line do if echo $line | grep -q 'Started'; then echo 'Server Started' exit 0 fi done }
这是我能想出的最好的答案
对于read,tail -f logfile |放一个超时读-t 30行 这将涵盖我可以想到的所有情况(服务器挂起,没有输出,服务器退出,服务器正确启动)。 不要忘记在服务器之前启动你的尾巴。 tail -n0 -F logfile 2>/dev/null | while read -t 30 line 即使不存在,-F也会读取该文件(出现时开始阅读)。 -n0不会读取文件中的任何内容,因此您可以继续附加到日志文件,而不是每次都覆盖它,并且可以对其进行标准的日志轮换。 编辑: function startServer() { touch logfile # 30 second timeout. sleep 30 & timerPid=$! tail -n0 -F --pid=$timerPid logfile | while read line do if echo $line | grep -q 'Started'; then echo 'Server Started' # stop the timer.. kill $timerPid fi done & startJavaprocess > logfile & # wait for the timer to expire (or be killed) wait %sleep } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |