grogo212
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!
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
(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
"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
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
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
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.....
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