Apache may sometime crash or stop for some other reasons. I had this issue with one of the server where Let’s Encrypt post hook does start Apache for whatever reasons.

So i thought it would be better to have some bash script which runs every minute and check Apache status and start it if its dead/inactive.

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 Ubuntu 16.04 server.


STARTAPACHE="systemctl start apache2"

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

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/apache.sh