Restart cygrunsrv service on failure

René Berber
Thu Oct 20 21:55:00 GMT 2005

Glen Burson wrote:
> I'm having real trouble getting my service to restart on failure, using the
> Windows service "Recovery" options. I have set: -
> First Failure "Restart the Service"
> Windows logs in the Event Viewer that the service terminated (gracefully,
> error code 0) but doesn't restart it.
> I have tried exiting from the script using a non-zero code (I've tried lots
> of different ones) but although the non-zero exit code is logged in the
> Event Viewer, it still doesn't restart it.
> I've also tried the install option "-n" which tells Windows that the service
> should never terminate but, although I now get an error in the Event Viewer,
> it still doesn't restart it!
> The ONLY way I've managed to get Windows to restart the service is to kill
> the cygrunsrv process before the child script terminates. Killing the child
> script does not work either.
> It seems to be related to how the process terminates but I seem to crack it!
> Thanks in advance for any suggestions. I've seen some postings that imply
> that people have this working but no examples.

I never tried exactly what you want, it looks like Windows doesn't cooperate so
its good to know not to waste time trying that.

One way I have a server set up is by installing a script (that watches the real
server) as the service, the interesing part of the script is this:

# Keep checking that it is working every TIME secs
while true; do
  sleep $TIME
  ( $DAEMON stop
    sleep 1
    rm -f $SOCKET
    $DAEMON start

I know this is not what you asked, here I have a program that tests to see if
the daemon is not responding and the script restarts the daemon in that case.

Back to your test, I think you are using the wrong exit codes.  Windows does not
use the Unix like codes 0, 1, etc.   I don't remember Windows codes but they
where something like (exit_code << 8 | windows_flags) that means 256 instead of 1.

René Berber

Unsubscribe info:
Problem reports:

More information about the Cygwin mailing list