Command Line Run Service

Hello,

How can I check if a service is running on the computer and if not start it?  I also need to do the same to stop it if it is running.

Im trying to add this to the Pre-build event command line in Visual Studio win service project.

Thx
grogo212Asked:
Who is Participating?
 
johnb6767Connect With a Mentor Commented:
See below.... Example using the Net Logon service.....
REM change service name of course, to either the actual name in the registry, or the Display Name in qoutes....
sc query "netlogon" | find /i "stopped"
if %errorlevel%==0 net start "netlogon"

Open in new window

0
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
Or you can do it with one line using || to run the next command if the previous command showed an error (in this case the FIND command NOT finding the service name in the output of "NET START" and therefore the service was not started:

net start | find /i "DNS client" || net start "DNS Client"

if DNS client is started it appears in the output of NET START so FIND finds it and errorlevel is 0 so it does nothing.

Another approach is... just start it anyway and hide any errors:

NET START "Your service" 2>NUL

Steve
0
 
Steve KnightIT ConsultancyCommented:
From the other way to stop if then :

NET START | find /i "DNS Client" && NET STOP "DNS Client"

will stop the stated service if it is started.  Again you could just do

NET STOP "Your service" 2>NUL
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
grogo212Author Commented:
jhon, this works for me:
sc query "SnapWinService" | find /i "stopped"
if %errorlevel%==0 net start "SnapWinService"

however this does not:
sc query "SnapCasaWinService" | find /i "started"
if %errorlevel%==0 net stop "SnapCasaWinService"

what would I need to stop it if it is running.

dragon,
This works in cmd:
NET START | find /i "SnapWinService" && NET STOP "SnapWinService"

but when I put it into Visual Studio and do a build it returns error:
The command "NET START | find /i "SnapWinService" && NET STOP "SnapWinService"" exited with code 1.

This one works in VS:
NET STOP "SnapWinService" 2>NUL

But only if it is already running.  If it is not running I get this error when I build:
The command "NET STOP "SnapWinService" 2>NUL" exited with code 2.      

Again, I need this to work from the Build Events settings of a Windows Service Project in Visual Studio.

Thanks!

0
 
Steve KnightIT ConsultancyCommented:
Presumably this is because your project is looking at the return code.  Try adding & EXIT 0 on the end and wrapping the rest in (), i.e.

(NET START | find /i "SnapWinService" && NET STOP "SnapWinService") & EXIT 0

etc. to reset the error level back to be returned as 0 (no error).

Steve
0
 
johnb6767Commented:
"however this does not:
sc query "SnapCasaWinService" | find /i "started"
if %errorlevel%==0 net stop "SnapCasaWinService"

Had to change  the search criteria....
sc query "SnapCasaWinService" | find /i "running"
if %errorlevel%==0 net stop "SnapCasaWinService
0
 
Steve KnightIT ConsultancyCommented:
surely that will still set the errorlevel for exit from the fond command... though fixed in the same way with exit 0 ATT the end

0
0
 
johnb6767Commented:
If it finds the string "running, then yes it is a 0 errorlevel, which would call the 'net stop'..... Both methods posted are this way.....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.