0

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.

10
  • 1
    Any particular reason you are not using apachectl status? Commented Apr 18, 2016 at 4:05
  • 1
    may I know why downvote? The question follows every rule of SO Commented Apr 18, 2016 at 4:15
  • 1
    @tripleee sometimes things don't happen the way you want them to be, may be I don't want to use the already existing tools, did you ask? even if I'm trying to reinvent the things, you were never compelled to answer. Pls don't exercise your vote unnecessarily. Be productive than being critic. Commented Apr 18, 2016 at 6:23
  • 1
    Opposing downvotes is not the way to get fewer of them. Please familiarize yourself with Stack Overflow before attempting to dictate the behavior of others. Commented Apr 18, 2016 at 8:11
  • 2
    I'm not opposing any one of them, if you're down voting, be mindful of giving feedback. Commented Apr 18, 2016 at 8:15

1 Answer 1

2

Try this:

#checking if Apache is running or not

if ! pidof apache2 > /dev/null
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
    if pidof apache2 > /dev/null
    then
        message="Apache restarted successfully"
    else
        message="Restart Failed, try restarting manually"
    fi
    mailx -s "$message" -r "$SENDEREMAIL" "$NOTIFYEMAIL"
fi

Note: every mailx line had a trailing -$, < /$, or <$ -- these looked like typos and were deleted.

Sign up to request clarification or add additional context in comments.

2 Comments

The grep -v grep is a common antipattern. You want ps auxw | grep -q '[a]pache2' or better yet pidof apache2
I'd considered pidof before, believe it or not, but got confused while reading the manual if there were any permission conditions on the OP's system that might make it fail. I'd not heard of that cure for grep -v grep before now, looked it up, great stuff, Tnx! Suggested code edits pending...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.