Skip to content

Relaunch Apache automatically if inactive in CentOS 7

Few days ago i wrote about how to start/restart Apache if it is inactive in Ubuntu. Today i am going to share a bash script for CentOS 7. It is mostly the same except few changes.

Login to your server and create apache.sh file in /opt/httpd (you can choose other dir for storing the script). Create httpd dir 1st(mkdir httpd).

This script is tested on CentOS 7 server.

#!/bin/sh

RESTART="systemctl start httpd"
SERVICE="httpd"
LOGFILE="/opt/httpd/autostart-apache2.log"

#check for the word inactive in the result of status
if systemctl status httpd | grep -q inactive
then
echo "starting apache at $(date)" >> $LOGFILE
$RESTART >> $LOGFILE
else
echo "apache is running at $(date)"
fi

If you want to log the else statement too, add >> $LOGFILE at the end of the echo line. I personally don’t need it as it will spam the logs.

To run this script every minute via cron:

* * * * * sh /opt/httpd/apache2.sh

Published inCentOSLinuxWeb Server

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
Theme by Compete Themes. Enhanced for The Random Bits only. © The Random Bits 2018.