Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a custom service that I want to monitor with monit. When the process fails I want to copy the log to a shared file system and restart the service. Something like the following but I am not not sure what. Any hints would be appreciated.

check process pipeline with pidfile /var/run/pipeline.pid
   start program = "/sbin/start pipeline"
   stop  program = "/sbin/stop pipeline"
   if 10 restarts within 10 cycles then timeout
   # Not sure what to write next
   if <service has failed> 
      restart and 
      exec "/bin/bash -c 'cp /var/log/upstart/pipeline.log /nfs/logs/`hostname`.`date +'%m-%d-%Y_%H.%M.%S'`.log'"
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

I would write a small bash script containing the desired event actions. Call that script from Monit.

It's cleaner, more modular and will behave more predictably. The same idea applies to cron jobs.

For instance, from the Monit examples page, would you rather have to support this:

check directory httpd_core with path /var/crash/core if changed timestamp then exec "/bin/bash -c 'if [ /bin/cat /tmp/monit_httpd_core.tmp | head -1 != /bin/ls /var/crash/core/core.httpd* | tail -1 ]; then /usr/bin/gdb -x /etc/gdb.batch /usr/sbin/httpd /bin/ls /var/crash/core/core.httpd* | tail -1 | tee /tmp/monit_httpd_core.tmp | mail -s httpd_crash admin@foo.bar webmaster@foo.bar; fi'"

or this:

check directory httpd_core with path /var/crash/core   if changed
timestamp then exec script.sh

Where script.sh contains all of the ugliness.

share|improve this answer
    
OK, thanks for your response. Just for my own edification, does that mean that monit doesn't support multiple actions on the same event (fail)? Naively, perhaps, I assumed this would be possible. –  Atlas1j Mar 7 '13 at 13:50
    
@Atlas1j Monit can support multiple events, but it's cleaner to use a script. –  ewwhite Mar 7 '13 at 14:02
    
Perfect. Thanks for helping me grok best practices with monit. –  Atlas1j Mar 7 '13 at 14:38
    
Actually the OP is correct, monit will only take 1 'action' for each 'test'. mmonit.com/monit/documentation/monit.html#action To send alerts and run scripts, you need to use exec as that one action. –  Eddie Jul 3 at 13:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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