"monit restart <service>" how do I know when it's done restarting?

Tim picture Tim · Dec 20, 2013 · Viewed 13.1k times · Source

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?

Answer

Tomas picture Tomas · Jan 5, 2014

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).