Welcome to the new Linux Foundation Forum!

Script to auto restart a service in linux

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

Comments

  • mfillpotmfillpot Posts: 2,180
    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.
  • Thanks Karma

    Can u please tell me any websites where i can get such scripts.
  • 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.
  • mfillpotmfillpot Posts: 2,180
    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?
  • 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.
  • gomergomer Posts: 158
    I wouldn't rely on the pid file. I've had services die unexpectedly and leave pid files and lock files behind.

    I would write a script that checks the output of ps for the presence of and state of the application, and then kick off the init script if it's missing, and kill and then restart it if it got zombied somehow.
Sign In or Register to comment.