how to programmatically restart WAMP or Apache?
As part of some automated deploy + test scripts I use to verify programming done for a site, I have some scripts which update Apache's config files. I would like to programmatically restart WAMP so the changes take effect. Is there a good way to do this?
The scripts are powershell.
This is whats in my apac开发者_JAVA百科he bin folder:
iconv
ab.exe
abs.exe
ApacheMonitor.exe
apr_dbd_odbc-1.dll
apr_ldap-1.dll
dbmmanage.pl
htcacheclean.exe
htdbm.exe
htdigest.exe
htpasswd.exe
httpd.exe
httxt2dbm.exe
libapr-1.dll
libapriconv-1.dll
libaprutil-1.dll
libeay32.dll
libhttpd.dll
logresolve.exe
openssl.exe
php.ini
php5isapi.dll
php5ts.dll
rotatelogs.exe
ssleay32.dll
wintty.exe
zlib1.dll
You can use this command to restart Wamp, Apache, MySQL services:
To start services
NET START wampapache
NET START wampmysqld
To stop services
NET STOP wampapache
NET STOP wampmysqld
For mariaDB, replace wampmysqld
with wampmariadb
.
For 64 bits: append 64 to the service names.
Simple execute command:
httpd.exe -k restart
ps. this is my wathdog for windows
@echo off
:loop
timeout /t 30 /nobreak
REM .
tasklist /FI "IMAGENAME eq php-cgi.exe" 2>NUL | find /I /N "php-cgi.exe">NUL
if "%ERRORLEVEL%"=="1" goto Process_NotFound
tasklist /FI "IMAGENAME eq httpd.exe" 2>NUL | find /I /N "httpd.exe">NUL
if "%ERRORLEVEL%"=="1" goto Process_NotFound
goto loop
:Process_NotFound
TASKKILL /F /IM php-cgi.exe
TASKKILL /F /IM httpd.exe
ping 127.0.0.1 -n 2
Apache -k start
ping 127.0.0.1 -n 3
cls
php.exe -r "$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://server.name/'); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec($ch);"
ping 127.0.0.1 -n 3
ab.exe -n 10 -c 3 http://server.name/
goto loop
- CTRL+R -> Type (Command) -> Right Mouse -> Run Administrator
- Go to your wamp aptech bin folder eg : D:\wamp\bin\apache\apache2.4.9\bin>
- Type httpd.exe -d (Show All apache parameter command )
- httpd.exe -k start -n wampapache64
- httpd.exe -k stop -n wampapache64
- httpd.exe -k restart -n wampapache64
Graphical Instruction :
Step First:
Step Second:
I ended up writing some code to find the "wampapache" service and restarting it.
public static void ResetApache()
{
ServiceUtil.RestartService("wampapache", 10000);
}
...
public class ServiceUtil
{
public static void RestartService(string serviceName, int msTimeout)
{
ServiceController service = new ServiceController(serviceName);
int startTicks = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(msTimeout);
if (service.Status != ServiceControllerStatus.Stopped
&& service.Status != ServiceControllerStatus.StopPending)
{
service.Stop();
}
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
int midTicks = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(msTimeout - (midTicks - startTicks));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
//int finalTicks = Environment.TickCount;
//var totalTime = TimeSpan.FromTicks(finalTicks - startTicks);
//Console.WriteLine("Reseting process took " + (totalTime.TotalMilliseconds/1000.0) + " seconds.");
}
}
精彩评论