금번 프로젝트에서 Monit 이라는 모니터링 프레임워크를 사용하게 되었다.
해당 프레임워크를 사용하여 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
결과확인.
반응형
최근댓글