2

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.

5
  • Don't. Use a monitoring tool (like monit or supervisord or runit or systemd or any of the handful of others). – Etan Reisner Jan 21 '16 at 21:01
  • @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." – guettli May 27 '16 at 4:38
  • @guettli Interesting. That seems like an awfully small hair to be splitting to me but <shrug>. – Etan Reisner May 27 '16 at 4:52
  • @EtanReisner please elaborate. I can only guess what you mean - I don't like guessing :-) – guettli May 27 '16 at 5:04
  • @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.) – Etan Reisner May 27 '16 at 11:32
5

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
3
  • 1
    /etc/init.d/ will sooner or later disappear. – guettli Jul 12 '16 at 15:01
  • 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. – NarūnasK Jul 16 '16 at 1:45
  • @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. – dhaupin Oct 17 '16 at 19:10
2

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).

2
  • 1
    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. – luciusodio Jul 25 '18 at 11:35
  • Is this solution useful for you? luciusodio and Seva Kobylin – Dhaduk Mitesh Oct 6 '18 at 5:22
1

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
1
  • 1
    /etc/init.d/ will sooner or later disappear. – guettli Jul 12 '16 at 15:02
1

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.