ev8zzg
Last Updated: November 18, 2016
·
2.921K
· mcansky
892a174c18182a2a3157e51cd071d1cf

Restarting a service through monit with a restart.txt file

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.

Say Thanks
Respond

4 Responses
Add your response

14556
7d1caf9df777b3b2cf474ff743494335

I like being able to invoke Monit from my deploy user, but like you I don't want to type a password.

So I've put this user in the sudoers list like this :

Cmnd_Alias MONIT = /usr/bin/monit
my_user ALL=(ALL) NOPASSWD: MONIT

This way I can use sudo without a password but only for Monit.

That said, I also like your solution for better separation between Monit supervised services. With my approach, anybody who has access to Monit can mess with all the services. With your approach it's possible to limit users to specific services since they can be restricted to their parent directories.

over 1 year ago ·
14559
892a174c18182a2a3157e51cd071d1cf

@jlecour : interesting solution too.

over 1 year ago ·
17959
U tzamky normal

Hi, I adopted your solution, however i was not 100% happy with it, and instead I've chosen another solution of using curl to make calls to the embedded http server(which is set to just listen for local connections) to invoke stop & start for services.
More details here http://balamaci.ro/monitoring-your-java-processes/

over 1 year ago ·
28325

Hi Thomas,
I'd like using monit for manually shutdown or reboot of my server. How can I adapt your script?

12 months ago ·