2012-09-13 2 views
2

응용 프로그램 인스턴스가 언제 생겼는지 인식 할 수있는 셸 스크립트를 얻으려고합니다. 그렇게하면 명령을 계속 실행할 수 있습니다. 이 일을하더라도, 그것은 몇 가지 문제를 해결하지 않을,로그 메시지가 성공할 때까지 루프 쉘 스크립트

#/bin/bash 

startApp.sh 

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

sleep 5 

done 

echo "App up" 

:하지만

나는 이런 식으로 뭔가 될 생각을 해 봤는데 앱에서 '아무튼 어떤 경우

  • t는, 얼마나 내가 로그 라인을 캡처하고
  • 를 반향 수있는 방법까지
  • 응용 프로그램을 가져 오는 오류가 발생하면 어떻게
  • 을 기다립니다 올

나는 가깝거나 더 좋은 방법이 있습니까? 나는 이것이 다른 관리자가 극복해야만하는 것이라고 생각합니다.

편집 :

내가 슈퍼 유저에 발견

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 

이 나의 유일한 문제는 그것이 결코 수도 출구이다. 최대 시간 내에 추가 할 수있는 방법이 있습니까?

답변

4

첫 번째 대답은 뻔했으나 내가 생각했던 모든 것을 설명하지 않았다 확인 할 수 우연히 있다.

여기 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 
나는 그것에 대해 생각
+0

statup에 실패하면 어떻게됩니까? –

+0

글쎄요, 이것은 오래됐지만 시작이 실패하면 콘솔에서 Ctrl + C를 눌러 종료해야합니다. 궁극적으로 --pid 플래그는 sleep proc가 180 초 후에 종료되고 꼬리를 종료시켜 결국 전체 스크립트가 종료되도록합니다. – tpederson

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)) 
+0

, 내 유일한 관심사는 그 Java 프로세스를로드 할 때 매초마다 수백 줄을 출력 할 수 있습니다. 따라서이를 보상하기에 충분히 높게 설정하면 이전에 다시 시작하여 거짓 긍정을 줄 수 있습니다. – tpederson

관련 문제