Cron은 자동으로 bash 스크립트를 실행하고, 1개의 bash 스크립트를 실행한 다음 다른 스크립트와 무결성 검사를 실행합니다.

Cron은 자동으로 bash 스크립트를 실행하고, 1개의 bash 스크립트를 실행한 다음 다른 스크립트와 무결성 검사를 실행합니다.

그래서 제3자가 개발한 사용자 정의 내부 애플리케이션이 있습니다. 애플리케이션이 실행되는 동안 "screen -ls" 명령을 사용하여 애플리케이션이 실행 중인지 확인할 수 있습니다. 화면이 Rails 및 Freeswitch에서 작동하는 한 애플리케이션이 제대로 실행되고 있음을 알 수 있습니다.

애플리케이션 관련 서비스를 중지하는 특정 bash 스크립트와 애플리케이션 관련 서비스를 시작하는 두 번째 스크립트가 있습니다.

내 질문은 이 두 스크립트를 결합하여 응용 프로그램을 하나의 스크립트로 다시 시작하는 방법입니다. 다음과 같이 작동합니다.

  1. 스크립트 1 실행 - 애플리케이션 중지
  2. 응용프로그램이 닫힐 때까지 기다립니다(화면 프로세스가 더 이상 실행되지 않음).
  3. 스크립트 2 실행 - 애플리케이션 시작
  4. 응용 프로그램이 시작될 때까지 기다리십시오
  5. 레일과 프리 스위칭 프로세스가 실행 중인지 확인하려면 "스크린" 소켓을 확인하세요. 그렇지 않은 경우 1단계로 돌아가서 반복하세요.

이제 애플리케이션을 다시 시작하세요.

  1. /tools/stop_app.sh를 통해 수동으로 중지 스크립트를 실행했습니다.
    • 그런 다음 터미널에 출력하여 서비스가 중단되었음을 표시합니다.
    • 완료되면 터미널 프롬프트로 돌아갑니다.
  2. 이제 /tools/start_app.sh를 통해 수동으로 시작 스크립트를 실행합니다.
    • 아무것도 출력되지 않지만 완료되면 터미널 프롬프트로 돌아갑니다.
  3. 그런 다음 screen -ls를 실행하여 애플리케이션의 모든 서비스가 실행 중인지 확인했습니다. (때때로 freeswitch와 같은 서비스가 시작되지 않을 수 있습니다.)
  4. 그렇지 않은 경우 중지/시작 스크립트를 다시 실행합니다.

왜 내가 모든 것을 스크립트에 넣지 않는지 물어볼 수도 있습니다. 이 맞춤형 앱은 매우 까다롭고 개발자의 지원이 제한되어 있으므로 개발자가 제공하는 정확한 도구를 사용해야 합니다. 따라서 1개의 스크립트는 개발자가 제공한 2개의 별도 스크립트를 호출합니다.

온전성 검사란 Ruby 및 Freeswitch 스크린이 실행 중인지 확인하기 위해 "스크린" 프로세스를 검사하는 것을 의미합니다. cron을 사용하여 매주 이 애플리케이션 다시 시작을 자동화하고 싶습니다.

bash 스크립트라고 말할 때 bash나 shell이라고 말하는 것이 맞는지 확실하지 않습니다. Ubuntu Linux에 일반적으로 기본적으로 설치되는 언어라면 스크립팅 환경 설정이 없습니다.

답변1

다음과 같이 할 수 있어야 합니다.

#/usr/bin/env bash

## We will use this function later to check if 
## everything has been correctly started.
function check_if_init_OK {
    ## You will need to edit this to add whatever services
    ## you have to check for.
    c=0; ## This is a counter, initialized to 0

    ## For each of the service names you are interested in.
    ## to add more, just put them after freeswitch, separated by a
    ## space they way they are now (e.g. a b c).
    for service in freeswitch foo bar baz; do

      ## Every time this loop is executed, $service will be
      ## one of the services you put in the list above. The
      ## script will run screen -ls and search for the name of the
      ## service. If it finds it, it will increment the counted $c by one.
      ## That is the meaning of '&&' in bash, x &&y means do y if x 
      ## was successful.
      screen -ls | grep $service >/dev/null 2>/dev/null && let c++; 
    done
    ## This just makes the function return $c which at this point
    ## will be how many of the the services you gave in the list have 
    ## been found.
    echo $c
}

## Run the first script -> stop app
script1.sh &

## Wait until it has stopped running
while screen -ls | grep script1.sh; do sleep 1; done 

## Run the second script -> start app and wait 15 seconds
script2.sh && sleep 15

## Check that everything has started OK. The function
## will return the number of services of interest that
## are up and running. While this is less than the number
## of services of interest, re-run script2.sh and check again.
## This loop will run the check_if_init_OK function until the 
## number returned (the number of running services of interest) is
## 3. You should change the 3 to reflect the actual number of services 
## you are looking for. So, as long as some services have not started,
## run script1.sh and then script2,sh and check if this time eveything
## has started OK. This loop will only exit when everything is working OK.
while [ "$(check_if_init_OK)" -ne 3 ];do
   script1.sh &&  script2.sh
done

관련 정보