|
Answer» Hi,
I'm new to dos batch scripting. I'm just trying to write a script that will check the status of a service and then depending on the status, give the option to either start or stop it. I have the following to check the current status. The problem is that it works find when the service is running. If I stop the service and then run this script, it doesn't work. DOS window comes up and quickly disappears.
If the service is running already and I run the script, it works. While the script is running, if I stop the service manually and then press any key to check the status again, it still shows that the service is running, which is incorrect.
Could you kindly show me what I'm doing wrong. As I'm new to this, I'm sure I'm missing something simple. Thanks for your help. Code: [Select]@echo off
set svc= apache2.2
:main call:check_services val echo %val% pause goto:main
:check_services
sc query %svc% | find "RUNNING">nul && set svcRunning= Yes
if %svcRunning% == Yes ( set %~1= Running ) else ( set %~1=Not Running )
goto:eofIn terms of pure syntax, these things need fixing...
Code: [Select]set svc= apache2.2 Remove space before apache unless you really have a reason to need it
Code: [Select]call:check_services val 1. Insert space after call and before :check_services 2. What is 'val' doing there?
Code: [Select]sc query %svc% | find "RUNNING">nul && set svcRunning= Yes Remove space before Yes (see above)
Code: [Select]if %svcRunning% == Yes ( Enclose %svcRunning% and Yes in quotes, and remove the spaces
Code: [Select]set %~1= Running ? You cannot set variables like this!
However, I think you have made things too complicated for yourself. All that CALL stuff. Too many variables. So many extra possibilities for confusion. Simpler is better. Why not do it this way:
1. Set your message to "not running" or "no" or "0" whatever. 2. Check if the service is running. 3. If it is running, change the message to "running" or "yes" or "OK" or "1" or whatever. 4. If it is not running, leave it alone. 5. Test the message & perform actions depending on the result.
Code: [Select]@echo off set svc=apache2.2 set svcRunning=not running sc query %svc% | find "RUNNING">nul && set svcRunning=running
Now you can do your checking
Code: [Select]echo Service %svc% is %svcrunning%. if "%svcRunning%"=="running" ( REM do stuff if it is running REM can be more than 1 line ) else ( REM do stuff if it is not running REM can be more than 1 line )
Personally I often use the & symbol to join 2 lines together if they are meant to always follow each other, like this
Code: [Select]set svcRunning=not running&sc query %svc% | find "RUNNING">nul && set svcRunning=running Some tips sincerely meant to be helpful...
-- avoid using chains of variables like this
Code: [Select]if "%something%"=="%something_else%" set variable=OK if "%variable%"=="OK" do_something when you could just do
Code: [Select]if "%something%"=="%something_else%" do_something of course if you need to test more than once then %variable% serves some purpose, but if it doesn't then it's just pointless code. I think I learned this habit when BASIC programming in the 1980s when memory was expensive (64K was a lot!) and since every variable used some memory, you had to be sparing. I know that memory is cheap nowadays but the more simple and readable you make your code, the easier it will be for to figure out what's going on, and the LESS time you will spend unravelling things when your code does not do what you want. Especially if you have a dozen variables all with similar names!
In short, get your ideas straight about what you want the batch to do, and then write the code, rather than trying to do both at once. Don't (as you sometimes see on here!) write the code first and work out what you want it to do afterwards.
Hi Dias,
Thanks so much for all the tips, suggestions and most importantly, time. You are right, I made it a little more complicated than it should've been. I CLEANED it up a bit.
Code: [Select]call:check_services val I was trying to pass parameter to check_services function so that I don't have to run multiple query line in there and use val INSTEAD in one line by accessing this val as "%~1" in check_services function. This way I can call that function multiple times and I won't have to have separate variables for each. But I think, that's too complex for batch. So, I got rid of that idea and now have two lines of query in check_services(since I'm checking for two different services).
I actually found my initial problem. I NEEDED to reinitialize a variable because Code: [Select]sc query %svc% | find "RUNNING">nul && set svcRunning= Yesthe above will only set svcRunning to "Yes" only when the service is running. And while the batch script is running, if I manually stop the service, then my script doesn't set svcRunning to "No". It uses the old value which is still set to "Yes". This is why, when I manually stop the service and call the check_services again, it wasn't showing the correct results.
Could you kindly tell me what is wrong with the following. Since I'm new to this, I pretty sure I just don't have the correct syntax. When my script reaches this if statement, it just bombs out. If I have one condition in the if statement, it works fine. Googling right now to find out how to COMBINE two if statements into one. Haven't found anything yet.
Code: [Select]if "%svc1_Running%"=="No" && "%svc2_Running%"=="No" ( rem show option to start both services ) else ( rem show option to stop both services ) Thanks again for your help.Quote from: pman on May 31, 2008, 12:27:15 PM Code: [Select]call:check_services val
I was trying to pass parameter to check_services function so that I don't have to run multiple query line in there and use val instead in one line by accessing this val as "%~1" in check_services function.
OK but it should be %val%
QuoteCould you kindly tell me what is wrong with the following.
&& doesn't do what you think it does.
This should work...
Code: [Select]if "%svc1_Running%"=="No" ( if "%svc2_Running%"=="No" ( rem show option to start both services goto next ) )
REM show option to stop both services
:next
code]
[/quote]
|