Cronjob to check and restart service if dead

15,014

Solution 1

service_ck.sh

#!/bin/bash
STATUS=$(/etc/init.d/service_name status)
# Most services will return something like "OK" if they are in fact "OK"
test "$STATUS" = "expected_value" || /etc/init.d/service_name restart

Change file permissions:

chmod +x service_ck.sh

Update your crontab:

# min   hour    day month   dow cmd
*/1 *   *   *   *   /path/to/service_ck.sh

Solution 2

Sorry for waking up a sleeping thread, but as many of the answers no-longer work and I found this page while looking, I figured I'd add my solution here:

Create script check_service.sh and set SERVICENAME as desired:

#!/bin/bash

SERVICENAME="WHATEVER_SERVICE_YOU_WANT"

systemctl is-active --quiet $SERVICENAME
STATUS=$? # return value is 0 if running

if [[ "$STATUS" -ne "0" ]]; then
        echo "Service '$SERVICENAME' is not curently running... Starting now..."
        service $SERVICENAME start
fi

Make the script executable:

chmod +x check_service.sh

Finally, add the script to Root's crontab by running sudo crontab -e:

# min   hour    day month   dow cmd
*/1 *   *   *   *   /full/path/to/check_service.sh

Save the crontab, and wait patiently!

Solution 3

You can use special software like monit for this case. It can check your daemons , restart it if needed and send you alerts. Another good option -- it can stop try to restart service after N fails (for example if service cannot start).

Solution 4

If you save this as a bash script it will be a one-liner that you can call from cron. This will restart Apache if it's not in the process list returned by pgrep.

Obviously this assumes that you have pgrep. Adjust your command to restart accordingly.

If Apache is running but not responsive, that is a different issue. You'd have to check that some endpoint is responding (and responding correctly) within a specified timeout, etc.

#!/bin/bash

RESTART="/etc/init.d/httpd restart"
PGREP="/usr/bin/pgrep"
HTTPD="httpd"

$PGREP ${HTTPD}

if [ $? -ne 0 ] # if apache not running 
then
 # restart apache
 $RESTART
fi
Share:
15,014
User
Author by

User

Internet entrepreneur

Updated on June 07, 2022

Comments

  • User
    User almost 2 years

    I want a 1 liner that can check and restart services, such as Apache if they are inactive/dead.

    I want to put it in crontab and run it every minute to make sure the service is still running.

    • Etan Reisner
      Etan Reisner over 8 years
      Don't. Use a monitoring tool (like monit or supervisord or runit or systemd or any of the handful of others).
    • guettli
      guettli almost 8 years
      @EtanReisner systemd has watchdog support (restart service if alive-ping is not send to systemd), but it does not restart the service if a check-service-alive-script fails:: lists.freedesktop.org/archives/systemd-devel/2016-May/… "That's out of the scope for systemd really. That's monitoring, and I am not convinced having custom check function support in systemd is really appropriate, this should be implemented outside of systemd really."
    • Etan Reisner
      Etan Reisner almost 8 years
      @guettli Interesting. That seems like an awfully small hair to be splitting to me but <shrug>.
    • guettli
      guettli almost 8 years
      @EtanReisner please elaborate. I can only guess what you mean - I don't like guessing :-)
    • Etan Reisner
      Etan Reisner almost 8 years
      @guettli I meant the claim that there's a meaningful difference between restarting on "alive" failure (and other exits) and on other (check-based) conditions. Yes, there's a difference but if (as according to the next post in the thread) "[a]dministrators usually create type timer units" to do this sort or monitoring then clearly they see it as a need that could easily get built in. (But I'm not a systemd dev so my opinion doesn't really matter.)
  • guettli
    guettli almost 8 years
    /etc/init.d/ will sooner or later disappear.
  • guettli
    guettli almost 8 years
    /etc/init.d/ will sooner or later disappear.
  • NarūnasK
    NarūnasK almost 8 years
    That's true, but currently systemd maps init.d to its services pretty well and until it will completely replace sysv, init.d start/stop scripts will and should live and thrive.
  • dhaupin
    dhaupin over 7 years
    @NarūnasK should live and thrive for a limited lifespan until depreciated modes/paths/methods are no longer supported. Also, there is this: something.service is not a native service, redirecting to systemd-sysv-install. Technically, although its familiar and backward compat, it's confusing and fragmented on modern installs -- not symlinked in a systemd way, don't show up in the right lists/places, etc. BTW you don't need this script/cron at all if you use systemd built in auto-restart abilities: Restart=always which should be available on pretty much every distro from here on out.
  • luciusodio
    luciusodio almost 6 years
    Looks like something vety usefull, but it will be very nice if you provide some manuals or documentation for this monit tool. Could you please add some links? Because I tryed to find something myself, but didn't succeed.
  • Dhaduk Mitesh
    Dhaduk Mitesh over 5 years
    Is this solution useful for you? luciusodio and Seva Kobylin