我正在使用Monit来监控系统.我有一个我想要监视的python文件,我知道我需要创建一个包装脚本,因为python不会生成pid文件.我按照本网站上的说明操作,但是我无法启动脚本.我之前从未创建过包装器脚本,所以我认为我的脚本中有错误.来自monit的日志说"启动失败"
Monit规则
check process scraper with pidfile /var/run/scraper.pid
start = "/bin/scraper start"
stop = "/bin/scraper stop"
Run Code Online (Sandbox Code Playgroud)
包装脚本
#!/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
Run Code Online (Sandbox Code Playgroud)
小智 7
使用exec将用exec'd程序替换shell ,这不是你想要的,你希望你的包装器脚本启动程序并在返回之前将其分离,将其PID写入文件以便以后可以停止.
这是一个固定版本:
#!/bin/bash
PIDFILE=/var/run/scraper.pid
case $1 in
start)
source /home
# Launch your program as a detached process
python /home/scraper.py 2>/dev/null &
# Get its PID and store it
echo $! > ${PIDFILE}
;;
stop)
kill `cat ${PIDFILE}`
# Now that it's killed, don't forget to remove the PID file
rm ${PIDFILE}
;;
*)
echo "usage: scraper {start|stop}" ;;
esac
exit 0
Run Code Online (Sandbox Code Playgroud)