Nowadays we tend to automate a lot of things. Yet in some cases monit handles the daemons and their stop / start events.
As a result, often we require to enter a password to pass the sudo. I don't really like that, it stops me from automating or just firing the deploy from somewhere.
Nginx and other services have been supporting a neat way to restart processes : a simple file existence check. I like that.
And well Monit can actually do something similar.
With old monit you need 2 files : the monitrc script and a restart script :
# monitrc file check file restart_resque with path /tmp/restart_resque.txt start program = "/usr/local/bin/resque_restart.sh" if timestamp > 1 minute then start check file stop_resque with path /tmp/stop_resque.txt start program = "/usr/local/bin/resque_stop.sh" if timestamp > 1 minute then start # script #!/bin/bash if [[ -f /tmp/restart_resque.txt ]]; then monit restart resque monit restart resque_scheduler rm /tmp/restart_resque.txt fi
Newer versions let you do that in a single file :
check file restart_resque path /tmp/restart_resque.txt if not exist then exec "/bin/bash -c 'echo dne > /dev/null'" else if succeeded then exec "/bin/bash -c 'monit restart resque && monit restart resque_scheduler && rm /tmp/restart_resque.txt'" check file stop_resque path /tmp/stop_resque.txt if not exist then exec "/bin/bash -c 'echo dne > /dev/null'" else if succeeded then exec "/bin/bash -c 'monit stop resque && monit stop resque_scheduler && rm /tmp/stop_resque.txt'"
If you are using capistrano you'll need to add something like the following :
run "touch /tmp/restart_resque.txt"
And voila. No more passwords.
Note : that's one way to do it, quite happy to hear other people ideas and tricks on the topic.