Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script: to check if Apache server is up and running

Tags:

bash

shell

apache

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

like image 737
user4943236 Avatar asked Oct 25 '25 01:10

user4943236


2 Answers

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
like image 52
JuliSmz Avatar answered Oct 27 '25 16:10

JuliSmz


Use this:

service apache2 status

Or this:

service --status-all | grep apache2
like image 35
Marcelo Ávila de Oliveira Avatar answered Oct 27 '25 16:10

Marcelo Ávila de Oliveira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!