금번 프로젝트에서 Monit 이라는 모니터링 프레임워크를 사용하게 되었다.

mmonit.com/monit/

 

Easy, proactive monitoring of processes, programs, files, directories, filesystems and hosts | Monit

Open Source Monit is free, open source software. You can redistribute Monit and/or modify Monit under the terms of the GNU Affero General Public License (AGPL). The license only applies if you plan to distribute Monit to third parties. The reason we use AG

mmonit.com

 

해당 프레임워크를 사용하여 python 모듈을 모니터링하는 예제를 남겨본다.

 

monit 에서 사용하는 모니터링 설정파일을 작성해준다.

$ vi /etc/monit.d/application_name

as uid 1000 은 uid 가 1000 인 user 에서 실행시킨다.

with timeout 30 seconds 는 30 초까지 응답하지 않으면 오류로 처리한다.

check process application with pidfile /var/run/application.pid
   start = "/home/user/app/app.sh start"
        as uid 1000 with timeout 30 seconds
   stop = "/home/user/app/app.sh stop"

 

monit 에서 python 모듈을 실행해주는 shell 파일을 작성해준다.

$ vi /home/user/app/app.sh

#!/bin/bash

PIDFILE=/var/run/application.pid

case $1 in
   start)
       echo "Command Start"
       # Launch your program as a detached process
       /usr/local/bin/python3.7 /home/user/app/application.py
   ;;
   stop)
      kill `cat ${PIDFILE}`
      # Now that it's killed, don't forget to remove the PID file
      rm ${PIDFILE}
   ;;
   *)
      echo "usage: app.sh {start|stop}" ;;
esac
exit 0

 

application.py 에서 pid 파일을 생성해준다.

def writePidFile(path):
    pid = str(os.getpid())
    currentFile = open(path, 'w')
    currentFile.write(pid)
    currentFile.close()

 

monit 을 리로드해주고!

$ service monit reload

 

결과확인.

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기