结束尾-f在shell脚本启动 [英] Ending tail -f started in a shell script
问题描述
我有以下。
- 一个Java进程日志写入标准输出
- shell脚本启动Java进程
- 另一个shell脚本,执行previous之一,日志重定向
- 我检查与
尾-f
的成功消息命令的日志文件。
即使我退出0在code我不能结束尾-f
的过程。
这并不让我的脚本来完成。是否有猛砸这样做的任何其他方式?
在code看起来像下面这样。
函数startserver的(){
触摸日志文件
startJavaprocess>日志文件和放大器; 尾-f日志文件|而读线
做
如果回声$线| grep的-q已启动;然后
回声服务器已启动
退出0
科幻
DONE
}
最好的答案我可以想出是这样
- 把超时的读取,
尾-f日志文件|阅读-t 30行
- 与尾开始
- PID = $
,这样当bash的过程已经完成,它会退出
这将覆盖我能想到的(服务器挂起没有输出,服务器将退出,服务器正常启动)。所有病例
不要忘了服务器之前,开始你的尾巴。
的/ dev / null的|; 尾-n0 -F日志文件2 - 而读-t 30线
在 -F
将读的文件,即使它不存在(开始读它出现时)。已经在文件中的 -n0
不会读什么,这样你就可以继续追加到日志文件,而不是每次都覆盖它,并以标准的日志旋转就可以了。
编辑:的结果
好了,一个相当粗糙的解决方案,如果你使用的尾巴。有可能使用别的东西,但尾巴更好的解决办法,但我得到了它送给你,尾巴让你断了管道的相当不错。 A'三通',这是能够处理SIGPIPE可能会更好地工作。 Java进程积极做好与某种类型的IM活着消息的文件系统下降可能是更容易等待。
函数startserver的(){
触摸日志文件 第30秒超时。
睡30安培;
timerPid = $! 尾-n0 -F --pid = $ timerPid日志文件|而读线
做
如果回声$线| grep的-q已启动;然后
回声服务器已启动
#停止计时器..
杀$ timerPid
科幻
完成&功放; startJavaprocess>日志文件和放大器; #等待计时器到期(或被杀)
等待%睡眠
}
I have the following.
- A Java process writing logs to the stdout
- A shell script starting the Java process
- Another shell script which executes the previous one and redirects the log
- I check the log file with the
tail -f
command for the success message.
Even if I have exit 0 in the code I cannot end the tail -f
process.
Which doesn't let my script to finish. Is there any other way of doing this in Bash?
The code looks like the following.
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
}
The best answer I can come up with is this
- Put a timeout on the read,
tail -f logfile | read -t 30 line
- Start tail with
--pid=$$
, that way it'll exit when the bash-process has finished.
It'll cover all cases I can think of (server hangs with no output, server exits, server starts correctly).
Dont forget to start your tail before the server.
tail -n0 -F logfile 2>/dev/null | while read -t 30 line
the -F
will 'read' the file even if it doesn't exist (start reading it when it appears). The -n0
won't read anything already in the file, so you can keep appending to the logfile instead of overwriting it each time, and to standard log rotation on it.
EDIT:
Ok, so a rather crude 'solution', if you're using tail. There are probably better solutions using something else but tail, but I got to give it to you, tail gets you out of the broken-pipe quite nicely. A 'tee' which is able to handle SIGPIPE would probably work better. The java process actively doing a file system drop with an 'im alive' message of some sort is probably even easier to wait for.
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
}
这篇关于结束尾-f在shell脚本启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!