【问题标题】:Loop shell script until successful log message循环 shell 脚本,直到成功记录消息
【发布时间】:2012-09-13 19:49:29
【问题描述】:

我正在尝试获取一个 shell 脚本来识别应用实例何时出现。这样它就可以继续发出命令了。

我一直在想它会是这样的:

#/bin/bash

startApp.sh

while [ `tail -f server.log` -ne 'regex line indicating success' ]
do

sleep 5

done

echo "App up"

但是,即使这有效,也无法解决一些问题:

  • 如果应用不上来怎么办,要等多久
  • 如果启动应用时出现错误怎么办
  • 如何捕获日志行并回显它

我接近了吗,还是有更好的方法?我想这是其他管理员必须克服的问题。

编辑:

我在超级用户那里找到了这个

https://superuser.com/questions/270529/monitoring-a-file-until-a-string-is-found

tail -f logfile.log | while read LOGLINE
do
   [[ "${LOGLINE}" == *"Server Started"* ]] && pkill -P $$ tail
done

我唯一的问题是它可能永远不会退出。有没有办法增加最大时间?

【问题讨论】:

    标签: shell


    【解决方案1】:

    好的,第一个答案很接近,但没有考虑到我认为可能发生的所有事情。

    我从这个链接改编了代码:

    Ending tail -f started in a shell script

    这是我想出的:

    #!/bin/bash
    
    instanceDir="/usr/username/server.name"
    serverLogFile="$instanceDir/server/app/log/server.log"
    
    function stopServer() {
    
        touch ${serverLogFile}
    
        # 3 minute timeout.
        sleep 180 &
        local timerPid=$!
    
        tail -n0 -F --pid=${timerPid} ${serverLogFile} | while read line 
        do
            if echo ${line} | grep -q  "Shutdown complete"; then
              echo 'Server Stopped'
              # stop the timer..
              kill ${timerPid} > /dev/null 2>&1
            fi
        done &
    
        echo "Stoping Server."
        $instanceDir/bin/stopserver.sh > /dev/null 2>&1
    
        # wait for the timer to expire (or be killed)
        wait %sleep
    
    
    }
    
    function startServer() {
    
        touch ${serverLogFile}
    
        # 3 minute timeout.
        sleep 180 &
        local timerPid=$!
    
        tail -n0 -F --pid=${timerPid} ${serverLogFile} | while read line 
        do
            if echo ${line} | grep -q  "server start complete"; then
              echo 'Server Started'
              # stop the timer..
              kill ${timerPid} > /dev/null 2>&1
            fi
        done &
    
        echo "Starting Server."
        $instanceDir/bin/startserver.sh > /dev/null 2>&1 &
    
        # wait for the timer to expire (or be killed)
        wait %sleep
    
    }
    
    stopServer
    startServer
    

    【讨论】:

    • 启动失败会怎样?
    • 嗯,这有点老了,但如果启动失败,你应该在控制台上看到它,然后按 ctrl-c 退出。最终 --pid 标志会看到 sleep proc 在 180 秒内退出,并终止 tail,这最终会导致整个脚本退出。
    【解决方案2】:

    好吧,tail -f 永远不会退出,所以这不是你想要的。

    numLines=10
    timeToSleep=5
    until tail -n $numLines server.log | grep -q "$serverStartedPattern"; do 
      sleep $timeToSleep
    done
    

    确保$numLines 大于服务器启动时$timeToSleep 期间可能显示的行数。

    这将永远持续下去;如果你只想留出这么多时间,你可以用这样的东西来限制循环迭代的次数:

    let maxLoops=60 numLines=10 timeToSleep=5 success=0
    for (( try=0; try < maxLoops; ++try )); do 
      if tail -n $numLines server.log | grep -q "$serverStartedPattern"; then
        echo "Server started!"
        success=1
        break
      fi
      sleep $timeToSleep
    done
    
    if (( success )); then
      echo "Server started!"
    else 
      echo "Server never started!"
    fi
    
    exit $(( 1-success ))
    

    【讨论】:

    • 我想了想,我唯一担心的是,在加载java进程时,它每秒可以输出数百行。因此,如果我将其设置得足够高以弥补这一点,它可能会捕获先前的重启并给出误报。
    猜你喜欢
    • 1970-01-01
    • 2021-02-08
    • 2017-11-13
    • 2012-03-11
    • 2021-11-10
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多