I'm using Monit to monitor a system. I have a python file I wish to also monitor, I understand that I need to create a wrapper script as python does not generate pid files. I followed the instructions on this site, however I have been unable to get the script to start. I've never created a wrapper script before, so I think I have an error in my script. The log from monit says "Failed to start"
Monit rule
check process scraper with pidfile /var/run/scraper.pid
start = "/bin/scraper start"
stop = "/bin/scraper stop"
wrapper script
#!/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
Using exec will replace the shell by the exec
'd program, this is not what you want to get here, you want your wrapper script to launch the program and detach it before returning, writing its PID to a file so it can be stopped later.
Here's a fixed version:
#!/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