Problem :
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.
Solution :
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
Well, i know it’s too late but someone can need it. I use cygwin with consoleZ.
-
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'
- Second, in .bash_functions copy this function
webstat()
{
red='e[0;41m'
green='e[0;42m'
NC='e[0m' # No Colorif [[ -n $(ps aux -W | grep httpd.exe) ]]; then
echo -e "Apache:t"${green}" RUNNING "${NC}
else
echo -e "Apache:t"${red}" STOPPED "${NC}
fiif [[ -n $(ps aux -W | grep mysqld.exe) ]]; then
echo -e "MySQL:t"${green}" RUNNING "${NC}
else
echo -e "MySQL:t"${red}" STOPPED "${NC}
fi
}
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.