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.
Join Stack Overflow to learn, share knowledge, and build your career.
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
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
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
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).
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
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
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!
monitorsupervisordorrunitorsystemdor any of the handful of others). – Etan Reisner Jan 21 '16 at 21:01