|
Answer» Hello all. On my pc, I am running Apache/PHP/Mysql. I have made two bat files.One to start the Apache/Mysql services and launch their associated tray icons(used for monitoring and other options), and another one to stop these services and terminate the tray icon interfaces.
I was wondering if it is possible to merge those two bats in one, so that when I launch it,it will start up all needed services etc, and when I launch it again,stop them. My code is included below:
start.bat _______
@START C:\AppServ\Apache2.2\bin\ApacheMonitor.exe @START C:\AppServ\MySQL\MYSQLT~1.0\MYSQLS~1.EXE @START C:\AppServ\Apache2.2\bin\httpd.exe -k start -n Apache2.2 @net start MySQL
stop.bat _______
@START C:\AppServ\Apache2.2\bin\httpd.exe -k stop -n Apache2.2 @net stop MySQL @taskkill /f /im ApacheMonitor.exe @taskkill /f /im MySQLSystemTrayMonitor.exe
Best Regards, George
virox There are (at least) 2 ways of approaching this ..
Create a file called (for example) ApacheStarted.txt and use it as a flag
When you RUN your batch, test for the presence of this file ... if found, you know the service is running, so delete the file and run the stop service code.
If it wasnt found, the servide isnt running, so run the start service code and create the file so you know next time that its already running.
The other method that comes to mind is to use TaskList, which spews out a list of the tasks currently running; capture this and see if your Apache task is in it (you could use findstr) - if it is, you know you need to stop it (and vice versa)
Grahamwhy not try something like this (if you find a bug please tell me)?:
@echo off :MENU echo. echo 1. Start service echo 2. Stop service echo 3. Exit echo. set /p num= Please enter a number... if %num% GEQ 4 cls & goto MENU if %num% EQU 0 cls & goto MENU if %num% EQU 1 cls & goto START if %num% EQU 2 cls & goto STOP if %num% EQU 3 cls & exit
:START @START C:\AppServ\Apache2.2\bin\ApacheMonitor.exe @START C:\AppServ\MySQL\MYSQLT~1.0\MYSQLS~1.EXE @START C:\AppServ\Apache2.2\bin\httpd.exe -k start -n Apache2.2 @net start MySQL
:STOP @START C:\AppServ\Apache2.2\bin\httpd.exe -k stop -n Apache2.2 @net stop MySQL @taskkill /f /im ApacheMonitor.exe @taskkill /f /im MySQLSystemTrayMonitor.exe
Thanks for your suggestions:)
@SOAP When I select "1" to start the service,it starts up everything properly,and after a few seconds terminates everything as well. Any idea why that happens? Thanks for the suggestion however:D It could be useful to me for some other situation.
@gpl This is what I ended up with. Works quite nicely
IF EXIST C:\apachestarted.txt ( @START C:\AppServ\Apache2.2\bin\httpd.exe -k stop -n Apache2.2 @net stop MySQL @taskkill /f /im ApacheMonitor.exe @taskkill /f /im MySQLSystemTrayMonitor.exe DEL C:\apachestarted.txt ) ELSE ( @START C:\AppServ\Apache2.2\bin\ApacheMonitor.exe @START C:\AppServ\MySQL\MYSQLT~1.0\MYSQLS~1.EXE @START C:\AppServ\Apache2.2\bin\httpd.exe -k start -n Apache2.2 @net start MySQL type C:\apachestarted.txt REM > C:\apachestarted.txt ) >NULL
Is there any way to make it so,that the file will only be created if all services are started properly,and delete only if all services are terminated? I know it's overkill, it would be interesting to know though
Again, thanks for your suggestions. Best Regards, George oh! oops! i forgot to add exit at the end of both of them, so when it started, it finished and then kept going into the commands to stop the server, here is the RIGHT code:
@echo off :MENU echo. echo 1. Start service echo 2. Stop service echo 3. Exit echo. set /p num= Please enter a number... if %num% GEQ 4 cls & goto MENU if %num% EQU 0 cls & goto MENU if %num% EQU 1 cls & goto START if %num% EQU 2 cls & goto STOP if %num% EQU 3 cls & exit
:START @START C:\AppServ\Apache2.2\bin\ApacheMonitor.exe @START C:\AppServ\MySQL\MYSQLT~1.0\MYSQLS~1.EXE @START C:\AppServ\Apache2.2\bin\httpd.exe -k start -n Apache2.2 @net start MySQL exit
:STOP @START C:\AppServ\Apache2.2\bin\httpd.exe -k stop -n Apache2.2 @net stop MySQL @taskkill /f /im ApacheMonitor.exe @taskkill /f /im MySQLSystemTrayMonitor.exe exitGeorge
Hmm - not impossible; I mentioned in my earlier post using TaskList to show what is running, so in your section that starts up the services, you could check that each of them are running; if any arent, display an error and jump back to the stop service section to close the others down
Grahamhmmm.... i'm thinking out loud but would errorlevel work in this situation?George,
I didn't take the time to adapt the following code to your application, but here is a batchfile that tests to see if notepad is running and then acts accordingly. (It is a working example of what gpl suggested.)
The key lines are the following, but you should try the test file to see how it works, so you can understand how to adapt it to your use.
Code: [Select]tasklist | findstr notepad.exe && goto Close_N_Pad tasklist | findstr notepad.exe || goto Start_N_Pad
If notepad is running, the first statement is true, so the goto is executed. If notepad is not running, the statement is false, so the goto is not executed. If notepad is not running, the second statement is true. The lines prefaced by :: are remarks, explaining what will occur in that section.
Copy/Paste the code into a text file and rename it to: Test_N_Pad.bat Then run the file with and without notepad running to see what it does.
Code: [Select]@echo off
: Test_N_Pad :: Check the tasklist to see if "Notepad.exe" is running :: Goto Close_N_Pad if "Notepad.exe" is running. tasklist | findstr notepad.exe && goto Close_N_Pad cls
:: Goto Start_N_Pad if N_Pad is not running. tasklist | findstr notepad.exe || goto Start_N_Pad cls
:Close_N_Pad :: This is an Error message that tells you to close "Notepad" and then loops :: back to the starting POINT, forcing you to close "Notepad" before continuing. cls echo.&echo.&echo.&echo.&echo. echo It appears that "Notepad" is still running! echo.&echo. echo Please save your work and close "Notepad" before continuing. echo.&echo.&echo.&echo.&echo.&echo.&echo.&echo.&echo.&echo. echo Press [Enter] to continue, echo.&echo.&echo.&echo. pause >nul cls goto Test_N_Pad
:Start_N_Pad :: Message CONFIRMS that "Notepad.exe is not running, then starts "Notepad.exe" cls echo.&echo.&echo.&echo.&echo. echo Made it to Start_N_Pad. echo.&echo. echo "Notepad" is NOT currently running. echo.&echo.&echo.&echo.&echo.&echo.&echo.&echo.&echo.&echo. echo Press [Enter] to continue, echo.&echo.&echo.&echo. pause >nul cls start "" "notepad.exe"
:End exit
There is no need to create a text file to test for. Simply test for one of the 4 processes using tasklist | findstr. Then goto the appropriate LABEL and start or end all 4 processes.
Or you could get really fancy and test for all four processes one at a time and start or stop the processes individually. (Ideally that would be the best way, but the other way should work as well.)
The goto label command in either of the tasklist lines can be replaced with any other command like you taskkill line if you want.
|