How to run apache server if it not already running

-1

I wonder if it is possible to check whether or not apache2 is running. If it is not running I want to execute it using shellscript

Currently I have added a function which I named run_apache which simply goes to the directory with apache2 starts it. I have added this function to my .bashrc file. It will however execute everytime I open a new shell, which I don't want to unless apache2 is not running already.

starcorn

Posted 2013-04-21T12:53:47.590

Reputation: 2 136

what system are you running, Debian? – gipi – 2013-04-21T13:08:31.233

okay, I missed some tags. And missed also to mention it is about Cygwin – starcorn – 2013-04-21T13:19:37.997

Answers

3

You can easily get apache2's status by launching

/etc/init.d/apache2 status

This will output one of these:

Apache2 is NOT running.
Apache2 is running (pid 10281).

It also happens to give you a valid return status, which you can use in a shell script (and discard its normal output with > /dev/null):

if /etc/init.d/apache2 status > /dev/null;
then echo "Apache already running";
else echo "Apache not running";
fi

I should add that there's no harm done from trying to start Apache when it's already running. So /etc/init.d/apache2 start will just say:

Starting web server: apache2httpd (pid 10281) already running

slhck

Posted 2013-04-21T12:53:47.590

Reputation: 182 472

If you're on Cygwin, I have no idea how Apache is controlled there. From what my research tells me you can use /usr/sbin/apachectl2 start similarly to the usual init.d scripts in regular Debian-like Linux distributions. – slhck – 2013-04-21T13:26:21.333

Thanks anyway I solved it after getting some inspiration that I can use if condition. So I used the following condition for it if ps aux | grep 'httpd2' -c > 0 which works for my usage. I'll mark this as accepted answer since I got an idea how to do from it. – starcorn – 2013-04-21T13:36:01.047

0

You could just add to your system's /etc/rc.local file your Apache startup command which would run it at the very end of the boot process.

There shouldn't be a need to check if it's running then because theoretically, it should stay running.

user183714

Posted 2013-04-21T12:53:47.590

Reputation:

0

Well, i know it's too late but someone can need it. I use cygwin with consoleZ.

  1. First, open .bash_aliases file and add these lines alias apacheup='cygstart --hide /g/xampp/apache_start.bat' alias apachedown='cygstart --hide /g/xampp/apache_stop.bat' alias mysqlup='cygstart --hide /g/xampp/mysql_start.bat' alias mysqldown='cygstart --hide /g/xampp/mysql_stop.bat' alias webup='apacheup && mysqlup' alias webdown='apachedown && mysqldown' alias webrestart='apachedown && mysqldown && apacheup && mysqlup'
  2. Second, in .bash_functions copy this function webstat() { red='\e[0;41m' green='\e[0;42m' NC='\e[0m' # No Color if [[ -n $(ps aux -W | grep httpd.exe) ]]; then echo -e "Apache:\t"${green}" RUNNING "${NC} else echo -e "Apache:\t"${red}" STOPPED "${NC} fi if [[ -n $(ps aux -W | grep mysqld.exe) ]]; then echo -e "MySQL:\t"${green}" RUNNING "${NC} else echo -e "MySQL:\t"${red}" STOPPED "${NC} fi }

Carlos Alberto Villamizar M.

Posted 2013-04-21T12:53:47.590

Reputation: 1