我需要编写一个 shell 脚本,在后台启动一个进程并解析它的输出,直到它检查输出中不包含任何错误。该进程将继续在后台运行,因为它需要侦听端口。如果进程输出包含错误退出脚本。
根据上一个进程的输出(它没有包含任何错误,该进程能够建立与 DB 的连接)运行下一个命令。
我已经尝试了许多关于堆栈溢出的建议,其中包括:
- https://unix.stackexchange.com/questions/12075/best-way-to-follow-a-log-and-execute-a-command-when-some-text-appears-in-the-log
- https://unix.stackexchange.com/questions/45941/tail-f-until-text-is-seen
- https://unix.stackexchange.com/questions/137030/how-do-i-extract-the-content-of-quoted-strings-from-the-output-of-a-command
/home/build/a_process 2>&1 | tee "output_$(date +"%Y_%m_%d").log"
tail -fn0 "output_$(date +"%Y_%m_%d").log" | \
while read line ; do
if [ echo "$line" | grep "Listening" ]
then
/home/build/b_process 2>&1 | tee "output_script_$(date +"%Y_%m_%d").log"
elif [ echo "$line" | grep "error occurred in load configuration" ] || [ echo "$line" | grep "Binding Failure" ]
then
sl -e
fi
done
问题是,尽管该过程包含我正在搜索的文本,但它仍在继续运行,它在解析凝视时卡住了,并且永远无法退出观看输出或拖尾。结果它无法执行下一个命令。