Last Updated: November 18, 2016
· mcansky

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
if [[ -f /tmp/restart_resque.txt ]]; then
  monit restart resque
  monit restart resque_scheduler
  rm /tmp/restart_resque.txt

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

4 Responses
Add your response


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

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 ·

@jlecour : interesting solution too.

over 1 year ago ·
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 ·

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

12 months ago ·