Linux – How to Automatically Restart a Service on Failure

linuxservicesocket

On Windows, you can set what should happen if/when a service fails. Is there a standard way of achieving the same thing on Linux (CentOS in particular)?

A bigger part of my question is: how do you handle sockets that have been left open – for example in TIME_WAIT, FIN_WAIT1, etc states.

At the moment if the service I am developing crashes, I have to wait for the sockets to clear or change the listen port before I can then manually restart it.

Thanks for your help.

Best Answer

monit is a great way to monitor and restart services when they fail--and you'll probably end up using this for other essential services (such as Apache). There's a nice article on nixCraft detailing how to use this for services specifically, although monit itself has many more functions beyond this.

As for the socket aspect, @galraen answered this spot on.