[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1610
  • Last Modified:

Checking service status with .bat file

Running Windows XP, I have three services whose status I want to automatically verify.  If the three services are running I simply want to proceed to the next line in the .bat file.  I tried piping text from sc.exe query <service_name> to a variable but that didn't do anything.  Assume we have services name sc1, sc2 and sc3.

Thanks
0
wls3
Asked:
wls3
  • 3
  • 2
1 Solution
 
tuttlepcCommented:
0
 
SteveGTRCommented:
Could do something like this:
@echo off
 
setlocal enabledelayedexpansion
 
for %%a in (sc1, sc2, sc3) do call :GETSRVSTAT "%%a"&if /i not "!status!"=="Running" goto :EOF
 
echo All services are running...
 
goto :EOF
 
:GETSRVSTAT
 
set status=
 
for /f "tokens=4" %%a in ('sc query "%~1" 2^>NUL^|findstr STATE') do set status=%%a
 
goto :EOF

Open in new window

0
 
wls3Author Commented:
This was a great lead.  All I had to do was modify the parameters for my situation and this worked perfectly.  Thanks.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
SteveGTRCommented:
Glad it worked :)
0
 
wls3Author Commented:
I found out the service name has spaces.  Aside from creating individual variables and contatinating them into a new variable, is there a way to work around this?  I got it to work doing the concat, but, it's ugly and doesn't seem efficient.
0
 
SteveGTRCommented:
No problem :)
@echo off
 
setlocal enabledelayedexpansion
 
for %%a in ("sc1", "sc2", "sc3") do call :GETSRVSTAT %%a&if /i not "!status!"=="Running" goto :EOF
 
echo All services are running...
 
goto :EOF
 
:GETSRVSTAT
 
set status=
 
for /f "tokens=4" %%a in ('sc query "%~1" 2^>NUL^|findstr STATE') do set status=%%a
 
goto :EOF

Open in new window

0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now