시간나는대로 틈틈히 정리 합시다~~!!!

[Linux] ShellScript를 이용하여 Application 프로세스 체크해서 재기동 시키기 본문

IT

[Linux] ShellScript를 이용하여 Application 프로세스 체크해서 재기동 시키기

열심히 정리하자!! 2021. 5. 18. 11:28
반응형

작업 사유

회사에서 주말에 갑자기 배치 서비스가 죽는 상황이 발생하였다.

그래서 프로세스 체크해서 활성화 하는 스크립트를 작성하였다.

 

 

생성 스크립트

- Process 체크 조건

   > 현재 프로세스 카운트가 0 이면 Application 시작 스크립트 호출

- ShellScript 작성

    # vi /var/local/application_sh/application_ps_check.sh

echo ""
echo "####### Application Process Check Start [`date '+%Y-%m-%d %H:%M:%S'`] ########"

echo "# "
echo "# - APPLICATION PROCESS BEFORE"
APPLICATION_BATCH=`ps -ef | grep -w application_batch.jar | grep -v grep`
echo "#   > APPLICATION_BATCH => $APPLICATION_BATCH"
echo "# "

echo "# "
echo "# - APPLICATION COUNT"
APPLICATION_BATCH_COUNT=`ps -ef | grep application_batch.jar | grep -v grep | wc -l`
echo "#   > APPLICATION_BATCH_COUNT => $APPLICATION_BATCH_COUNT"
echo "# "

echo "# "
echo "# - APPLICATION_PID"
APPLICATION_BATCH_PID=`ps -ef | grep application_batch.jar | grep -v grep | awk '{print $2}'`
echo "#   > APPLICATION_BATCH_PID => $APPLICATION_BATCH_PID"
echo "# "

echo "# "
echo "# - APPLICATION_BATCH_CHECK"
if [ "$APPLICATION_BATCH_COUNT" == 0  ]; then
        echo "#   > NOT FOUND APPLICATION_BATCH"
        /var/local/application_batch/bin/startup.sh
        echo "#   > STARTUP !!"
else
        echo "#   > FOUND APPLICATION_BATCH"
fi

echo "# "
echo "# - APPLICATION PROCESS AFTER"
APPLICATION_BATCH=`ps -ef | grep -w application_batch.jar | grep -v grep`
echo "#   > APPLICATION_BATCH => $APPLICATION_BATCH"
echo "# "

echo "# "
echo "####### Application Process Check End ######################################"
echo ""

 

 

로그 저장 스크립트

- ShellScript 작성

    # vi /var/local/application_sh/application_ps_check_log_save.sh

/var/local/application_sh/application_ps_check.sh >> /var/local/application_sh/logs/application_ps_check.log.`date +%Y%m%d` 2>&1

 

 

Crontab 등록

- Crontab 편집기로 내용 입력

    # crontab -e

# 1분마다 application batch, api  프로세스 체크
*/1 * * * * /var/local/application_sh/application_ps_check_log_save.sh

 

 

 

로그내용

- 이슈 발생한 경우

####### Application Process Check Start [2021-04-28 12:13:01] ########
#
# - APPLICATION PROCESS BEFORE
#   > APPLICATION_BATCH =>
#
#
# - APPLICATION COUNT
#   > APPLICATION_BATCH_COUNT => 0
#
#
# - APPLICATION_PID
#   > APPLICATION_BATCH_PID =>
#
#
# - APPLICATION_BATCH_CHECK
#   > NOT FOUND APPLICATION_BATCH
#   > STARTUP !!
#
# - APPLICATION PROCESS AFTER
#   > APPLICATION_BATCH => root     14183     1  2 April28 ?        12:13:02 /usr/local/java/bin/java -jar -Dspring.profiles.active=prod -Djennifer.config=/home/agent.java/conf/jennifer.conf -javaagent:/home/agent.java/jennifer.jar /var/local/application_batch/application_batch.jar
#
#
####### Application Process Check End ######################################

 

- 이슈 없는 경우

####### Application Process Check Start [2021-05-18 11:27:01] ########
#
# - APPLICATION PROCESS BEFORE
#   > APPLICATION_BATCH => root     14183     1  2 May06 ?        07:20:20 /usr/local/java/bin/java -jar -Dspring.profiles.active=prod -Djennifer.config=/home/agent.java/conf/jennifer.conf -javaagent:/home/agent.java/jennifer.jar /var/local/application_batch/application_batch.jar
#
#
# - APPLICATION COUNT
#   > APPLICATION_BATCH_COUNT => 1
#
#
# - APPLICATION_PID
#   > APPLICATION_BATCH_PID => 14183
#
#
# - APPLICATION_BATCH_CHECK
#   > FOUND APPLICATION_BATCH
#
# - APPLICATION PROCESS AFTER
#   > APPLICATION_BATCH => root     14183     1  2 May06 ?        07:20:20 /usr/local/java/bin/java -jar -Dspring.profiles.active=prod -Djennifer.config=/home/agent.java/conf/jennifer.conf -javaagent:/home/agent.java/jennifer.jar /var/local/application_batch/application_batch.jar
#
#
####### Application Process Check End ######################################
반응형
Comments