15

Our sysadmin recently switched to using monit, and so now when I want to restart a service, I'm supposed to use "monit restart <servicename>" instead of "/etc/init.d/<servicename> restart".

However, the monit command, when successful, produces no output and returns immediately. It seems like it doesn't actually do the restart until after it's next cycle.

I'm restarting the service because I made changes to it. I don't want to test my changes against the old instance. So I need to know when the restart is complete. I also would prefer it do the restart when I tell it to, and not when it gets around to it. I don't enjoy typing "ps aux | grep <myservicename>" over and over again while I wait.

Is there a way to make monit restart my service immediately?

Is there a way to make monit, or perhaps a wrapper around monit, block until the restart is finished?

Is there a way to make monit tell me that the restart was successful, or else that it failed?

Tomas
  • 57,621
  • 49
  • 238
  • 373
Tim
  • 4,999
  • 3
  • 24
  • 29

3 Answers3

21

Monit is a very silent program! In fact, monit commands tend to run and exit immediately because they run in the background! SO ... use the -v switch if you want to see what Monit is doing, and tail -f the log file and the -I if you want it to run in the foreground.

Synchronous Restarting

Use the -I option:

monit -I restart servicename

This will disable restarting in background, which is e.g. needed when your computer boots!

If you want to diagnose problems, add the verbose option -v:

monit -Iv restart servicename

Status Checking

To check the result, you could try several things:

1) Return value of monit

monit -I restart servicename
echo $?

Normally, $? should be zero upon success and non-zero otherwise. However not programs support it and there is no information on the manpage what is the exit status ($?) of monit. Try to test it.

2) Use status or summary commands

monit -I status

or

monit -I status servicename

or

monit -I summary

these commands will return the status on the output. You may select the command that works best for you and parse its output. Or, as in point 1), check the return value $? (it is not mentioned in the manpage).

King'ori Maina
  • 4,440
  • 3
  • 26
  • 38
Tomas
  • 57,621
  • 49
  • 238
  • 373
  • I think you might be right about the -I option (I was surprised, I thought that only applied to starting monit), but the -v option seems pretty useless. It doesn't tell me if my command was successful, but instead dumps pages and pages of information about every service monit is managing. – Tim Jan 08 '14 at 18:57
  • @Tim 1) so did the `-I` option help? 2) `-v` is not meant to return status, only to diagnose potential problems 3) For returning status, please refer to last paragraph of my answer. What does `$?` variable contain? What does `monit status servicename` tell you? – Tomas Jan 08 '14 at 19:20
  • @Tim, I have expanded my answer a bit to be more comprehensible. – Tomas Jan 09 '14 at 11:19
  • 1
    The `-I` option doesn't appear to do anything. Both with and without the option, the restart simply get's "scheduled" for the next cycle, and the restart command returns successfully immediately (I am using Monit 5.4) – Hubro Mar 22 '15 at 12:59
  • 1
    The -I flag doesn't do what you (and I want), it makes it so *monit* doesn't run in the background, not the commands monit receives. There might be a CLI wait flag added at some point http://nongnu.13855.n7.nabble.com/monit-start-stop-in-a-synchronous-mode-td190714.html#a197671 – mmrobins Jun 22 '15 at 19:53
1

One can wakeup the monit deamon by just running monit. This should help to reduce the wait to next cycle.

Do tail -f monit.log to see if the restart was successful or if it failed.

hrr
  • 1,807
  • 2
  • 21
  • 35
0

You can always use something like this

while test -f pidfile.pid && kill -0 $(cat pidfile.pid);
do 
  sleep 1; 
done;

while ! kill -0 $(cat pidfile.pid); 
do 
  sleep 1; 
done

this script wait while your process is being restarted.

jozol
  • 1
  • 1