Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers 2

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

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
add comment

Your Answer

 
discard

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.