Linux.com

Home Linux Community Forums Linux Distributions Red Hat Script to auto restart a service in linux

Script to auto restart a service in linux

Link to this post 19 Feb 10

Hi All,
may i please know if a script can be written to check the log messages and automatically restart a service.

Thanks in advance

Regards,
Eajaz

Link to this post 19 Feb 10

You can create a script file that is called by cron at whatever interval you find appropriate.

You can decide to have it analyze any of the below for indicators if the service should be restarted.
* the output of a log file to make decisions
* verify open ports if it is a network service
* check the output of lsof or ps if it is a system service

However when a service goes down it may be due to some configuration issue which will not be resolved with a service restart, rather than having it continually attempt to restart a service it may be safer to have it alert you of the failure so you can address it and have confidence in continued operations.

Link to this post 19 Feb 10

Thanks Karma

Can u please tell me any websites where i can get such scripts.

Link to this post 19 Feb 10

There is a software called nagios (http://www.nagios.org/) that monitors if a service is available on a computer and raises an action if not. I never used it, but maybe it is what you are looking for.

Link to this post 20 Feb 10

ajazshariff wrote:

Thanks Karma

Can u please tell me any websites where i can get such scripts.

If you are looking for a script rather than an application to manage the apps then first you need to know exactly what service you are evaluating to understand what tracking method bet fits your needs. Can you tell me what program/daemon you want to evaluate for automatic restart?

Link to this post 06 Mar 10

You could easily just monitor a PID file for the service if that service creates one. If the PID file is not as expected, send a message to the admin and try to restart the service. Executing ps with a search using grep to find a running process would also work. As was mentioned earlier, monitoring the messages log would probably be the least effective way of doing this. Just about any modern monitoring software (like Nagios mentioned earlier) will do this for you with little effort needed.

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board