This was requested by Darklord who wanted to monitor couchpotato, SickRage and other services for failure and restart them if they did
I use monit for these tasks on Wheezy which basically monitors your choice of pid files created by the init.d scripts for starting and stopping the services and restarts them if the pid has died or gone stale. On Jessie you need to use systemd scripts as I haven't been able to find init.d .pid files on Jessie, this is to some extent easier and will make a separate thread for that.
First install monit
Find the path of the pid file, usually they are in /var/run
You may have the pid file somewhere else which you can see in the /etc/default/service-name file (e.g. /etc/default/couchpotato) or in the init.d script itself (nano /etc/init.d/couchpotato).
Now to tell monit to monitor this file you need to open the monitrc file
Use Ctrl+V to get all the way to the bottom and add these lines, the first one defines the service name and path to the pid file
The other two lines are the commands to execute for starting and stopping the service should it be necessary
You can just follow that format for multiple services
Change the address for the monit web interface - where you can monitor the status of services - to listen on too just for security reasons. This is also in the /etc/monit/monitrc file
Add this under the set httpd port 2812 and line
Ctrl+X, Y and Enter to save then restart monit
You can test it by manually killing the CouchPotato service and then use the top command to see the python couchpotato process pop up again
For Plex on arm devices you will need to make a symlink to the plexmediaserver.pid
Create plex config
Paste
Save the file and restart monit
I use monit for these tasks on Wheezy which basically monitors your choice of pid files created by the init.d scripts for starting and stopping the services and restarts them if the pid has died or gone stale. On Jessie you need to use systemd scripts as I haven't been able to find init.d .pid files on Jessie, this is to some extent easier and will make a separate thread for that.
First install monit
Code:
sudo apt-get install monit -y
Find the path of the pid file, usually they are in /var/run
Code:
cd /var/run
ls
cd /run
ls
You may have the pid file somewhere else which you can see in the /etc/default/service-name file (e.g. /etc/default/couchpotato) or in the init.d script itself (nano /etc/init.d/couchpotato).
Now to tell monit to monitor this file you need to open the monitrc file
Code:
sudo nano /etc/monit/monitrc
Use Ctrl+V to get all the way to the bottom and add these lines, the first one defines the service name and path to the pid file
The other two lines are the commands to execute for starting and stopping the service should it be necessary
Code:
check process couchpotato with pidfile /var/run/couchpotato.pid
start program = "/etc/init.d/couchpotato start" with timeout 60 seconds
stop program = "/etc/init.d/couchpotato stop"
You can just follow that format for multiple services
Change the address for the monit web interface - where you can monitor the status of services - to listen on too just for security reasons. This is also in the /etc/monit/monitrc file
Add this under the set httpd port 2812 and line
Code:
use address 192.168.40.15 # only accept connection from local network, use 0.0.0.0 for web access
allow ip-address # allow localhost to connect to the server and
allow htpc:guides # require user 'htpc' with password 'guides'
Ctrl+X, Y and Enter to save then restart monit
Code:
sudo service monit restart
You can test it by manually killing the CouchPotato service and then use the top command to see the python couchpotato process pop up again
For Plex on arm devices you will need to make a symlink to the plexmediaserver.pid
Code:
ln -s /var/lib/plexmediaserver/Library/Application\ Support/Plex\ Media\ Server/plexmediaserver.pid /run/plexmediaserver.pid
Create plex config
Code:
nano /etc/monit/conf.d/plex
Paste
Code:
check process plexmediaserver with pidfile /run/plexmediaserver.pid
start program = "/etc/init.d/plexmediaserver start" with timeout 60 seconds
stop program = "/etc/init.d/plexmediaserver stop"
if failed host 127.0.0.1 port 32400 then restart
if failed url http://127.0.0.1:32400/web/index.html then restart
Save the file and restart monit