使用monit监控Python脚本时,要注意:start中创建单独的进程,并运行到后台,否则monit会认为程序超时。
主要有两种方式:
- 使用su命令
check process 1-wire with pidfile /tmp/1-wire.pid
start = "/bin/su - username -c 'cd /home/username/Code/pi-fishtank-sensors; /usr/bin/python3 1-wire.py >> /tmp/fishtank.log 2>&1 &'"
stop = "/usr/bin/killall python3"
- 把python启动命令封装到sh脚本中
check process scraper with pidfile /var/run/scraper.pid
start = "/bin/scraper start"
stop = "/bin/scraper stop"
scraper脚本内容
#!/bin/bash
PIDFILE=/var/run/scraper.pid
case $1 in
start)
echo $$ > ${PIDFILE};
source /home
exec python /home/scraper.py 2>/dev/null
;;
stop)
kill `cat ${PIDFILE}` ;;
*)
echo "usage: scraper {start|stop}" ;;
esac
exit 0