Hi guys, this is a litte tutorial about how we start and stop the Apache and MySQL in Windows NT (2000, XP, 2003) via a batch file script.

As we know in Windows NT based system Apache and MySQL installed as Windows Services. So we can stop and start it using NET command.

For more information about DOS command, type HELP at command prompt. I assuming that your MySQL service name is "mysql" and your Apache (Apache 2.0.x) service name is "apache2". If you want to chek it click Start > Run > services.msc > OK. Windows IS NOT Case Sensitive. Let's get started!.

1. Open your command prompt by clicking Start > Run > cmd > OK
2. Type edit then press ENTER to start the EDIT program.
3. Type code below

CODE
@ECHO OFF

if "%1"=="start" goto start_server
if "%1"=="stop"  goto stop_server
if "%1"=="" goto begin

:begin
TITLE ::BATCH SCRIPT::
ECHO ----------------------------------------------------------------------------------
ECHO USAGE OF THIS SCRIPT:
ECHO server.bat option1 [option2]
ECHO WHERE option1 = start/stop (start or stopping server)
ECHO      option2 = yes/no (close the command prompt or not [optional])
ECHO -----------------------------------------------------------------------------------
goto end

:start_server
TITLE ACTIVATING SERVER...
ECHO =================================================
ECHO       BATCH SCRIPT FOR ACTIVATING SERVER
ECHO    By Rio Astamal (masterio.trap17.com)
ECHO =================================================
ECHO _
ECHO Activating MySQL Service . . .
@NET START mysql
ECHO _
ECHO Activating Apache HTTP Server Service. . .
@NET START apache2
ECHO _
ECHO SERVER HAS BEEN STARTED!
DATE /T
TIME /T
goto end

:stop_server
TITLE STOPPING SERVER...
ECHO ==================================================
ECHO       BATCH SCRIPT FOR STOPPING SERVER
ECHO    By Rio Astamal (masterio.trap17.com
ECHO ==================================================
ECHO _
ECHO Stopping Apache HTTP Server Service...
@NET STOP apache2
ECHO _
ECHO Stopping MySQL Service. . .
@NET STOP
ECHO SERVER HAS BEEN SHUTTING DOWN!
DATE /T
TIME /T
goto end

:end
if "%2"=="yes" (
  exit
)


To save the file:
1. Press ALT-F > Save
2. Go to your Windows Directory, so we can start it from anywhere in command prompt
3. OK

Usage:
C:\>server.bat (to show the usage of the script)
C:\>server.bat stop yes (close the command prompt after finished the task)
C:\>server.bat start

EXPLAINATION:
If you create a batch file, Anything inside it is assumed as DOS command by command prompt. In that script, first, we make the ECHO command invisible by using @ECHO OFF command. Then we check the argument given by user. %1 is argument 1, %2 is argument 2 and so on. We use first argument(%1) to check what user want. Is he want to stop/start the server. We use argument2 (%2) to check wheter we close the command prompt or not after it.

TITLE is used for changing the title of command prompt title bar. For starting service We can use NET START [service_name]. In our script we use @NET START to prevent DOS for echoing the command. For stopping service we can use NET STOP [service_name].

SUMMARY
With a batch file we can do alot of thing not just stop/starting service. But we can use it for renaming many files at once, deleting temporer directory and many more. For more tutorial you can googling!!.

ENJOY...! biggrin.gif

 

 

 


Comment/Reply (w/o sign-up)