I set up ```monit```, a utility for monitoring services on a Unix system. My concrete use case is a FreeBSD machine, that runs "N2N Edge", a Peer-to-peer (P2P) virtual private network (VPN) software. ```monit``` pretty much automates the "have you tried turning it off and on again?" process. Configurations like the following illustrate this:
```bash
check process myproc
matching "myproc"
start program = "/etc/init.d/myprocstart"
stop program = "/usr/bin/killall myproc"
if cpu usage > 95% for 10 cycles then restart
```
<span id="more-3668"></span>In order to be monitored, every process needes a file containing the process id, when it runs (a so called pidfile). I used the following script to wrap the ```start_edge``` script, which in turn holds the actual command to start the VPN software:
```bash
#!/usr/local/bin/bash
case $1 in
start)
echo $$ > /var/run/edge.pid;
exec 2>&1 /root/start_edge 1>/tmp/edge.out
;;
stop)
killall edge;;
*)
echo "usage: edge {start|stop}" ;;
esac
exit 0
```
After ```pkg install monit``` an example config file can be found at ```/usr/local/etc/monitrc.sample```. I removed the ```.sample``` part of the file name and appended the following to the very end:
```bash
check process edge
pidfile /var/run/edge.pid
start program = "/usr/bin/edge start"
stop program = "/usr/bin/edge stop"
```
Make sure to check the config for obvious errors with ```monit -t```, reload the configuration and start the process:
```bash
monit reload
monit start all
```