Search code examples
monit

monit call exec on process recovery


What I would like to do, is the following:

  • if process-x fails (to (re)start) then execute cmd-x
  • if it recovers then execute cmd-y

For the alerting via E-mail, a notificaton is sent per default on recovery. For the exec method however, I can not find a way to make this work. If I try this in the monitrc:

check process proc_x with pidfile /var/run/proc_x.pid
   start program = "/bin/sh -c '/etc/init.d/Sxxproc_x start'"
   stop  program = "/bin/sh -c '/etc/init.d/Sxxproc_x stop'"
   if 3 restarts within 5 cycles then exec "<some error cmd>"
   else if succeeded then exec "<some restore cmd>"

this results in a "syntax error 'else'". If I remove the else line, the error command is called as expected. Apparently, the 'else' can not be used for the restarts test. But how can I add to execute a command is program starting succeeds or recovers?


Solution

  • I found a solution thanks to the answer to this topic:

    get monit to alert first and restart later

    The "if not exist for ..." with corresponding "else" did the trick for me to report the recover. The error report is separate. My monitrc code now:

    check process proc_x with pidfile /var/run/proc_x.pid
       start program = "/bin/sh -c '/etc/init.d/Sxxproc_x start'"
       stop  program = "/bin/sh -c '/etc/init.d/Sxxproc_x stop'"
       if 1 restart within 1 cycle then exec "<some error cmd>"
           repeat every 1 cycle
       if not exist for 3 cycles then restart
       else if succeeded 2 times within 2 cycles then exec "<some restore cmd>"