I'm trying to achieve the following steps using bash script:
1) Check the status of Apache Server
.
2) If it is up and running, do nothing. If it is not, then go to step 3.
3) If server is not running, then send a failure email first and restart the server
4) After restarting, check the status again, and send a confirmation email
Here is my code:
#checking if Apache is running or not
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? != 0 ]
then
mailx -s "Apache web server is down, Trying auto-restart" -$
# web server down, restart the server
sudo /etc/init.d/apache2 restart > /dev/null
sleep 10
#checking if apache restarted or not -- This is not working
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? = 0 ]
then
mailx -s "Apache restarted succesfully" -r "$SENDEREMAIL" "$NOTIFYEMAIL" < /$
else
mailx -s "Restart Failed, try restarting manually" -r "$SENDEREMAIL" "$NOTIFYEMAIL" <$
fi
fi
The code is working properly till step 3, and failing/not working on step 4 i.e. script is unable to check the status of the server after restart and sending a confirmation email. Can someone please let me know where I'm going wrong.