Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
what system are you running, Debian? – gipi Apr 21 '13 at 13:08
okay, I missed some tags. And missed also to mention it is about Cygwin – starcorn Apr 21 '13 at 13:19
up vote 3 down vote accepted

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";

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
share|improve this answer
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 Apr 21 '13 at 13:26
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 Apr 21 '13 at 13:36

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.

share|improve this answer

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 }
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.