This guide is intended for keeping your instance of bitcoind constantly running.
1. Install Monit
through your package manager or from the website.
2. Install the latest git ebuild of bitcoind that has pid support. You can tell if you have it by typing bitcoind --help and seeing -pid. You must have a version of bitcoind with pid support.
Bitcoin version 0.3.21 beta
bitcoin [options] <command> [params] Send command to -server or bitcoind
bitcoin [options] help List commands
bitcoin [options] help <command> Get help for a command
-conf=<file> Specify configuration file (default: bitcoin.conf)
-pid=<file> Specify pid file (default: bitcoind.pid)
3. Set your monit config file to the following. It should be in /etc/monitrc but check where your package manager installed it. Replace datadir and any other features you may use inside of the start program parameters. If you change the port, make sure you update the failed port section.
set daemon 60
set logfile /var/log/monit.log
check process bitcoind with pidfile "/var/run/bitcoind.pid"
start program "/usr/bin/bitcoind -pid=/var/run/bitcoind.pid -datadir=/root/.bitcoin -daemon"
stop program "/usr/bin/bitcoind stop"
if 3 restarts within 5 cycles then timeout
if failed port 8332 for 3 cycles then restart
4. Start monit. It may be in /etc/init.d/monit start or wherever your system installed it. Set it to startup as well so if the machine reboots it will start bitcoind automatically for you.
5. Test it out. Do a bitcoind stop and see if monit auto-restarts it. From now on, if port 8332 doesn't work, the program still stop bitcoind and restart it, which should solve any hanging that occurs due to random crashes.