2

I want monit to restart geth on crashes and send me alerts by email.

Here is what I'm trying to do.

My monit config at /etc/monit/conf.d/geth.cfg

# GETH
CHECK PROCESS geth with pidfile "/home/username/monit/geth.pid"
    start program = "/bin/bash -c '/home/username/monit/geth.sh start'" as uid username as gid username
    stop program = "/bin/bash -c '/home/username/monit/geth.sh stop'" as uid username as gid username
    if not exist then alert
    if not exist then restart

And the start/stop script /home/username/monit/geth.sh

#!/bin/bash
PID='/home/username/monit/geth.pid'
LOGS='/home/username/monit/geth.log'
KEYS='\--fast --rpc --rpcport 8545 --rpccorsdomain localhost --ipcpath /home/username/.ethereum/geth.ipc --cache=16'

case $1 in
   start)
       setsid geth "$KEYS" &> "$LOGS" & 
       echo $! > "$PID"
   ;;
   stop)
      cat "$PID"
      kill -SIGINT `cat "$PID"`
      rm "$PID"
   ;;
   *)
      echo "usage: geth.sh {start|stop}" ;;
esac
exit 0

I can run geth with sudo monit start geth, but monit cannot see geth's pid right. So it will not restart geth on crashes. The problems in my script:

  1. Cannot define geth's pid right. The geth.pid receives a process number which is higher by 2-4 numbers (e.g. 4678 instead of 4674).
  2. Cannot kill geth with -SIGINT, have to send -HUP signal instead.

I understand this is more like a Linux question, then Ethereum's, but I believe it may be very useful.

The closest howto's I found on geth monitoring are:

  • "$KEYS" shouldn't be in quotes in the start) clause. Works for me after removing those. – djvs May 4 '18 at 16:38
2

I did find a temporary working solution. It restarts geth just fine and sends alerts. But if you want to stop geth here you'll have to unmonitor it first with sudo monit unmonitor geth and then gracefully kill it with -SIGINT signal manually. Otherwise it gets killed with -HUP.

Monit config. /etc/monit/conf.d/geth.cfg

# GETH
CHECK PROCESS geth MATCHING "[g]eth.*fast"
    start program = "/bin/bash -c 'geth --fast --rpc --rpcport 8545 --rpccorsdomain localhost --cache=16 >/dev/null 2> /home/my-logs-folder/geth.log'" as uid username as gid username
    stop program = "/bin/bash -c 'kill -HUP $(ps aux | grep '[g]eth' | awk '{print $2}')'" as uid username as gid username
    if not exist then alert
    if not exist then restart

I believe the better approach is the script cited in the question, so I'm not marking this as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.