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.

user4943236
  • 5,914
  • 11
  • 27
  • 40
  • add "set -x" to the start of this, re-run it and add in that output – Dave Apr 18 '16 at 03:56
  • 1
    Any particular reason you are not using `apachectl status`? – tripleee Apr 18 '16 at 04:05
  • 1
    may I know why downvote? The question follows every rule of SO – user4943236 Apr 18 '16 at 04:15
  • I like the above comments. Also, seems like the status comparison maybe inaccurate for some bash/sh? `if [ "$?" -eq "0" ]` http://tldp.org/LDP/abs/html/comparison-ops.html – Colby Blair Apr 18 '16 at 04:30
  • I downvoted because this is flogging a dead horse. There are already adequate tools for this, and newbies trying to reinvent them poorly is not a good way to spend anybody's time. – tripleee Apr 18 '16 at 06:20
  • 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. – user4943236 Apr 18 '16 at 06:23
  • Yes, [I asked](http://stackoverflow.com/questions/36685209/bash-script-apache-server-is-running-or-not?noredirect=1#comment60959531_36685209), and you didn't answer. Did you look for duplicates? – tripleee Apr 18 '16 at 06:43
  • I did and deleted thereafter because of your down vote. If you don't have any suggestions on how to implement what I'm looking for, please don't comment – user4943236 Apr 18 '16 at 07:05
  • 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. – tripleee Apr 18 '16 at 08:11
  • 2
    I'm not opposing any one of them, if you're down voting, be mindful of giving feedback. – user4943236 Apr 18 '16 at 08:15

1 Answers1

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.

agc
  • 7,973
  • 2
  • 29
  • 50
  • The `grep -v grep` is a common antipattern. You want `ps auxw | grep -q '[a]pache2'` or better yet `pidof apache2` – tripleee Apr 18 '16 at 06:44
  • 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](http://stackoverflow.com/questions/9375711/more-elegant-ps-aux-grep-v-grep) for `grep -v grep` before now, looked it up, great stuff, Tnx! Suggested code edits pending... – agc Apr 18 '16 at 07:14