4

I am new to bash scripting and trying to figure out why the below script is outputting that Apache server is not running whereas it is running properly.

ps cax | grep httpd
if [ $? -eq 0 ]; then
 echo "Process is running."
else
 echo "Process is not running."
fi

I'm running it on Ubuntu 14.04.2 LTS

Also, how do I make changes to the script that this can test apache server installed on another machine. Kindly help

5
  • systemctl status apache2.service? Commented Apr 13, 2016 at 7:07
  • this is not working saying 'systemctl' not found Commented Apr 13, 2016 at 7:24
  • Try with sudo. I can't remember when Ubuntu started using systemd, but you could probably use service apache2 status. Commented Apr 13, 2016 at 7:50
  • 1
    also you have apachectl status Commented Apr 13, 2016 at 8:45
  • i'd go with apachectl status as it should be working in either OS version Commented Apr 13, 2016 at 9:54

8 Answers 8

9

This is a working sample of bash script which check the apache status, restart it automatically if down, and alert by telegram bot within unicode emoji.

#!/bin/bash
telegram=(xxxxx, yyyyyy)

if ! pidof apache2 > /dev/null
then
    # web server down, restart the server
    echo "Server down"
    /etc/init.d/apache2 restart > /dev/null
    sleep 10

    #checking if apache restarted or not
    if pidof apache2 > /dev/null
    then
        for i in "${telegram[@]}"
        do
        curl -s -X POST https://api.telegram.org/botxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyy/sendMessage -d chat_id="$i" -d text="`echo -e '\U0001F525'` Apache stoped on Molib Stage. Automatically restarted succesfully."
        done
    else
        for i in "${telegram[@]}"
        do
        curl -s -X POST https://api.telegram.org/botxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyy/sendMessage -d chat_id="$i" -d text="`echo -e '\U0001F525'` Apache stoped on Molib Stage. Automatically restart failed. Please check manually."
        done
    fi
fi
Sign up to request clarification or add additional context in comments.

2 Comments

What do the xxx and yyy, etc mean? I'm trying to work through the Telegram docs and it's not quite clear. Thanks!
@marklark sorry if I wasn't clear. xxxx, yyyy are the recipients of the alerts (telegrams ID's).
5

Use this:

service apache2 status

Or this:

service --status-all | grep apache2

1 Comment

+1 for service apache2 status. However, service --status-all | grep apache2 is not applicable to the OP's script: it returns $? == 0 if apache2 service is present but not running. ...though it is a good general advice!
2

Try and see - simply simplest, most didactic here and well working on Ubuntu 20.04:

  1. catching output of status to bash variable
  2. "if" status includes substring (from "Active:" statement) - do job you wanted
  3. "else" - do another job you defined
#!/bin/bash
servstat=$(service apache2 status)

if [[ $servstat == *"active (running)"* ]]; then
  echo "process is running"
else echo "process is not running"
fi

Comments

1

Instead of httpd try to grep "apache2". To be sure try to check services with the next command and decide the registered name of the apache webserver: service --status-all

3 Comments

what would be the output if I try ps cax | grep apache2
Something else than you want, often; that's why you are getting repeated replies urging you to give up on that already. Yours is a very frequent FAQ -- reinventing service scripts should rarely be necessary, unless you are the developer of the service in question.
I think is actually the correct answer to the question. The question is not "how do I check the status of apache"? The question is, "why is my script not working". This is probably the reason.
1

This work perfect in an old Debian. Remember to run with bash and not with sh.

In Centos replace with httpd.

#!/bin/bash
if [ $(/etc/init.d/apache2 status | grep -v grep | grep 'apache2 is running' | wc -l) > 0 ]
then
 echo "Process is running."
else
  echo "Process is not running."
fi

2 Comments

On line number 2 it should be grep 'apache2 is running' not 'Apache2 is running' small "a" not capital "A". Just an observation
Thanks @JosephBoladeCaxton-Idowu, I take your observation.
1

I put this together based on the above and made so can use other services.

Hope this helps.

#!/bin/bash
# Must be running as root or via sudo permissions to be able to restart
# Put your process name restart command line in
PROCESS_NAME=httpd
if ! pidof $PROCESS_NAME > /dev/null
then
    # web server down, restart the server
    echo "Server $PROCESS_NAME down"
    /usr/sbin/apachectl restart > /dev/null
    echo "Tried restart of $PROCESS_NAME. Waiting 10 seconds to settle."
    # wait ten
    sleep 10

    #checking if process restarted or not
    if pidof $PROCESS_NAME  > /dev/null
    then
        echo "$PROCESS_NAME was down but is now up."
    else
        echo "$PROCESS_NAME is still down. Please take some action."
    fi
else
    echo "Server $PROCESS_NAME up."
fi

Comments

0
## Plz run this script .. its working 
------------------------------------------------
ps cax | grep httpd
if [ $? -eq 1 ]
 then
 echo "Process is running."
else if [ $? -eq 0 ]
 echo "Process is not running."
fi
fi
----------------------------------------------

2 Comments

Welcome to Where Developers Learn, Share, & Build Careers! Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
You may try elif instead else if
0

This is menu driven one stop shell script in which you can check the firewall,apache or any other webservices ,you can start or stop the services just by choosing the option in below script

echo "welcome please select your options"
read choice
firewall=`sudo systemctl status firewalld`
apache=`sudo systemctl status apache2`
firewall1=`sudo systemctl stop firewalld`
apache1=`sudo systemctl stop apache2`
startrfirewall=`sudo systemctl start firewalld`
startapache=`sudo systemctl start apache2`

case $choice in
         1) status of the firewall is $firewall
                          ;;
         2) status of apache is $apache
                          ;;
         3) echo stop firewall by $firewall1
                          ;;
         4) echo stop apache by $apache1
                          ;;
         5) echo start firewall by $startrfirewall
                          ;;
         6) echo start apache by $startapache
                          ;;
         *) echo exit
esac

Comments

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.