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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

johnb6767Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve KnightIT 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
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.