Discussion:
Issuing 'SC Stop' for service - but waiting until stopped ?
(too old to reply)
(PeteCresswell)
2015-12-27 16:37:01 UTC
Permalink
I want to delete a bunch of IP cam output files, but do not want the
.BAT file to hang for such a long time because the cam server
has some files IN USE.

So I want to stop the service, delete the files, and then
re-start the service.

I do not want the service to start automagically while the
delete are in progress.

viz:
===============================================================
sc stop BlueIris
sc config BlueIris start= disabled

ERASE B:\Alerts\*.* /Q
ERASE B:\Clips \*.* /Q
ERASE B:\Clips_Stored\*.* /Q
ERASE B:\DB\*.* /F /Q

sc start BlueIris
sc config BlueIris start= auto

ECHO OFF
ECHO .
ECHO .
ECHO . ------------ Done! -------------
ECHO .
ECHO .
PAUSE
==================================================================

It's almost working, but not quite.

According to "Services.msc", the BlueIris service is left in a
"Stopping" state and attempts to re-start it or go at it
via the SC command do not work.

viz:
==================================================================
C:\BAT>sc stop BlueIris
[SC] ControlService FAILED 1061:

The service cannot accept control messages at this time.


C:\BAT>sc config BlueIris start= disabled
[SC] ChangeServiceConfig SUCCESS

C:\BAT>ERASE B:\Alerts\*.* /Q

C:\BAT>ERASE B:\Clips \*.* /Q

C:\BAT>ERASE B:\Clips_Stored\*.* /Q

C:\BAT>ERASE B:\DB\*.* /F /Q

C:\BAT>sc start BlueIris
[SC] StartService FAILED 1056:

An instance of the service is already running.


C:\BAT>sc config BlueIris start= auto
[SC] ChangeServiceConfig SUCCESS

C:\BAT>ECHO OFF
.
.
. ------------ Done! -------------
.
.
Press any key to continue . . .
==================================================================

Am I going at this the wrong way ?
--
Pete Cresswell
David E. Ross
2015-12-27 17:47:39 UTC
Permalink
Post by (PeteCresswell)
I want to delete a bunch of IP cam output files, but do not want the
.BAT file to hang for such a long time because the cam server
has some files IN USE.
So I want to stop the service, delete the files, and then
re-start the service.
I do not want the service to start automagically while the
delete are in progress.
===============================================================
sc stop BlueIris
sc config BlueIris start= disabled
ERASE B:\Alerts\*.* /Q
ERASE B:\Clips \*.* /Q
ERASE B:\Clips_Stored\*.* /Q
ERASE B:\DB\*.* /F /Q
sc start BlueIris
sc config BlueIris start= auto
ECHO OFF
ECHO .
ECHO .
ECHO . ------------ Done! -------------
ECHO .
ECHO .
PAUSE
==================================================================
It's almost working, but not quite.
According to "Services.msc", the BlueIris service is left in a
"Stopping" state and attempts to re-start it or go at it
via the SC command do not work.
==================================================================
C:\BAT>sc stop BlueIris
The service cannot accept control messages at this time.
C:\BAT>sc config BlueIris start= disabled
[SC] ChangeServiceConfig SUCCESS
C:\BAT>ERASE B:\Alerts\*.* /Q
C:\BAT>ERASE B:\Clips \*.* /Q
C:\BAT>ERASE B:\Clips_Stored\*.* /Q
C:\BAT>ERASE B:\DB\*.* /F /Q
C:\BAT>sc start BlueIris
An instance of the service is already running.
C:\BAT>sc config BlueIris start= auto
[SC] ChangeServiceConfig SUCCESS
C:\BAT>ECHO OFF
.
.
. ------------ Done! -------------
.
.
Press any key to continue . . .
==================================================================
Am I going at this the wrong way ?
Why not put a Pause after the Erase statements? Include some
human-oriented text in the Pause statement. Then when all erasing is
done, you hit the space bar on your keyboard to continue.
--
David E. Ross

Pharmaceutical companies claim their drug prices are
so high because they have to recover the costs of developing
those drugs. Two questions:

1. Why is the U.S. paying the entire cost of development while
prices for the same drugs in other nations are much lower?

2. Manufacturers of generic drugs did not have those
development costs. Why are they charging so much for generics?
Andy Burns
2015-12-27 19:26:24 UTC
Permalink
Post by (PeteCresswell)
I want to stop the service, delete the files, and then
re-start the service.
Loop with a sleep and repeatedly match the status with

sc query wudfsvc | find /i "state"

break out when it's no longer running, or error out after too many loops?
pjp
2015-12-27 20:46:43 UTC
Permalink
Post by Andy Burns
Post by (PeteCresswell)
I want to stop the service, delete the files, and then
re-start the service.
Loop with a sleep and repeatedly match the status with
sc query wudfsvc | find /i "state"
break out when it's no longer running, or error out after too many loops?
Isn't there a "/wait" option for cmd? Run the "stop service" command
under a called new shell from within the batch file with the wait
parameter. When it returns the service should be stopped or else some
"errorlevel" code should result that you can test how to continue.
Andy Burns
2015-12-27 22:23:27 UTC
Permalink
Post by pjp
Isn't there a "/wait" option for cmd?
There's start.exe /wait ....
Post by pjp
Run the "stop service" command
under a called new shell from within the batch file with the wait
parameter. When it returns the service should be stopped or else some
"errorlevel" code should result that you can test how to continue.
But you'll only be waiting until sc.exe has issued the stop command to
the Service Control Manager, not until it has taken effect, the service
could get stuck in "stopping" state ..
Mayayana
2015-12-27 22:37:03 UTC
Permalink
I don't know whether you want to deal with WMI.
It's a bit tedius and the object model is funky. But
it's an option that provides a more "comfy" set of
functions than using command line. WMI is designed
for this kind of thing, while BAT files are more of
a hack approach.
A VBScript example follows. The methods return
values, so you don't have to wait and wonder when
the service has stopped. The function doesn't return
until there's a result:

Dim WMI, oServ, Ret, StartName

' StartName = [service name here]
Err.Clear
Set WMI = GetObject("WinMgmts:")
Set oServ = WMI.Get("Win32_Service.Name='" & StartName & "'")
If (Err.number = 0) Then
If (oServ.State = "Running") Then Ret = oServ.StopService
'-- Success if Ret = 0
'do file deletes here....
Ret = oServ.StartService
'-- success if Ret = 0. Error codes listed in WMI help file.
End If
Set oServ = Nothing
Set WMI = Nothing

'----- end VBScript----

If necessary you can change the start mode
while working:

Ret = oServ.ChangeStartMode("disabled")
Ret = oServ.ChangeStartMode("manual")
Ret = oServ.ChangeStartMode("automatic")

You can also check the start mode:

sMode = UCase(oServ.StartMode)
' should return DISABLED, MANUAL or AUTOMATIC

All of that could be built into a single function
that returns a code to let you know whether
it worked and, if not, why.
JJ
2015-12-27 22:45:21 UTC
Permalink
Post by (PeteCresswell)
I want to delete a bunch of IP cam output files, but do not want the
.BAT file to hang for such a long time because the cam server
has some files IN USE.
So I want to stop the service, delete the files, and then
re-start the service.
I do not want the service to start automagically while the
delete are in progress.
===============================================================
sc stop BlueIris
sc config BlueIris start= disabled
ERASE B:\Alerts\*.* /Q
ERASE B:\Clips \*.* /Q
ERASE B:\Clips_Stored\*.* /Q
ERASE B:\DB\*.* /F /Q
sc start BlueIris
sc config BlueIris start= auto
ECHO OFF
ECHO .
ECHO .
ECHO . ------------ Done! -------------
ECHO .
ECHO .
PAUSE
==================================================================
It's almost working, but not quite.
According to "Services.msc", the BlueIris service is left in a
"Stopping" state and attempts to re-start it or go at it
via the SC command do not work.
==================================================================
C:\BAT>sc stop BlueIris
The service cannot accept control messages at this time.
C:\BAT>sc config BlueIris start= disabled
[SC] ChangeServiceConfig SUCCESS
C:\BAT>ERASE B:\Alerts\*.* /Q
C:\BAT>ERASE B:\Clips \*.* /Q
C:\BAT>ERASE B:\Clips_Stored\*.* /Q
C:\BAT>ERASE B:\DB\*.* /F /Q
C:\BAT>sc start BlueIris
An instance of the service is already running.
C:\BAT>sc config BlueIris start= auto
[SC] ChangeServiceConfig SUCCESS
C:\BAT>ECHO OFF
.
.
. ------------ Done! -------------
.
.
Press any key to continue . . .
==================================================================
Am I going at this the wrong way ?
In most cases, a service usually can't be stopped when something is still
using it.

There should be a software that's included by the cam hardware package that
is currently running. It may be like a common windows program that shows the
cam view, or runs in the background and sits on the tray. You may need to
close that software first. It might be the one that give the command to the
service to start the cam recording as well as stop it, gracefully.

If there isn't such software, check the cam's manual about stopping the cam
recording.

If there isn't any, which is the worst case, it means that the service is
supposed to be independent but has design flaw(s) where it can't properly
stop itself. There's one thing you can try. It may work or it may leave the
cam device unusable until the next system reboot (or hard reboot; if there's
also a hardware design flaw).

To stop:
Set the service to "disabled" using SC, then kill the service process using
TASKKILL.

To start:
Using SC, set the service to "demand" or "auto", then start the service.
(PeteCresswell)
2015-12-30 21:12:01 UTC
Permalink
Post by (PeteCresswell)
Am I going at this the wrong way ?
FWIW, here is what I have now - and which seems to work.

The main problem I ran in to before this was the Service getting left
in eternal "Stopping" mode - and therefore unreceptive to further
commands or even the Windows UI until the PC was rebooted.

Not so elegant as the other solutions.... but so far, so good.
===============================================================
:* =====================================
:* PURPOSE:
:* - To take the cam server offline
:* while we delete some of it's files
:*
:* - To delete all of the cam server's
:* Alert, Clip, and DB files
:*
:* - To bring the cam server back online
:* =====================================


:* ------------------------------
:* Kill the BI service

SC STOP BlueIris


:* ------------------------------
:* Wait 5 seconds to give STOP time
:* to do it's thing

CHOICE /N /T 5 /D Y


:* ------------------------------
:* Disable the BI service

SC CONFIG BlueIris START= disabled


:* ------------------------------
:* Wait 5 seconds to give CONFIG time
:* to do it's thing

CHOICE /N /T 5 /D Y


:* ------------------------------
:* Do the deed: Delete Clips and
:* Alerts, delete the Alert DB

ERASE B:\Alerts\*.* /Q
ERASE B:\Clips \*.* /Q
ERASE B:\Clips_Stored\*.* /Q
ERASE B:\DB\*.* /F /Q


:* ------------------------------
:* Re-start the BI service
sc config BlueIris start= auto
sc start BlueIris


ECHO OFF
ECHO .
ECHO .
ECHO . ------------ Done! -------------
ECHO .
ECHO .
PAUSE
===============================================================
--
Pete Cresswell
Paul
2015-12-31 04:58:37 UTC
Permalink
Post by (PeteCresswell)
Post by (PeteCresswell)
Am I going at this the wrong way ?
FWIW, here is what I have now - and which seems to work.
The main problem I ran in to before this was the Service getting left
in eternal "Stopping" mode - and therefore unreceptive to further
commands or even the Windows UI until the PC was rebooted.
Not so elegant as the other solutions.... but so far, so good.
===============================================================
:* =====================================
:* - To take the cam server offline
:* while we delete some of it's files
:*
:* - To delete all of the cam server's
:* Alert, Clip, and DB files
:*
:* - To bring the cam server back online
:* =====================================
:* ------------------------------
:* Kill the BI service
SC STOP BlueIris
:* ------------------------------
:* Wait 5 seconds to give STOP time
:* to do it's thing
CHOICE /N /T 5 /D Y
:* ------------------------------
:* Disable the BI service
SC CONFIG BlueIris START= disabled
:* ------------------------------
:* Wait 5 seconds to give CONFIG time
:* to do it's thing
CHOICE /N /T 5 /D Y
:* ------------------------------
:* Do the deed: Delete Clips and
:* Alerts, delete the Alert DB
ERASE B:\Alerts\*.* /Q
ERASE B:\Clips \*.* /Q
ERASE B:\Clips_Stored\*.* /Q
ERASE B:\DB\*.* /F /Q
:* ------------------------------
:* Re-start the BI service
sc config BlueIris start= auto
sc start BlueIris
ECHO OFF
ECHO .
ECHO .
ECHO . ------------ Done! -------------
ECHO .
ECHO .
PAUSE
===============================================================
Like Andy Burns suggestion, you can see how
Brink handles a service here.

http://www.tenforums.com/tutorials/24742-windows-update-reset-windows-10-a.html

:wuauserv
net stop wuauserv
echo Checking the wuauserv service status.
sc query wuauserv | findstr /I /C:"STOPPED"
if not %errorlevel%==0 (
goto wuauserv
)

:appidsvc
net stop appidsvc
echo Checking the appidsvc service status.
sc query appidsvc | findstr /I /C:"STOPPED"
if not %errorlevel%==0 (
goto appidsvc
)

I presume the %errorlevel% is coming from the
findstr call. And that being a tight loop,
might use a bit of CPU while it is checking
for the thing being STOPPED.

That code has no timeout in it, so if problems
arise, the user has to press <control>-C to
stop the script. And use the text of the "echo" line,
to figure out where it got stuck.

Paul
Andy Burns
2015-12-31 07:48:26 UTC
Permalink
Post by (PeteCresswell)
FWIW, here is what I have now - and which seems to work.
Clearly 5 seconds is better than 0 seconds, but in how many cases does
it need longer to work?

Might be better to disable it *before* stopping it, otherwise it could
get restarted while you're sleeping for the stop to take effect.
David E. Ross
2015-12-31 15:49:19 UTC
Permalink
Post by Andy Burns
Post by (PeteCresswell)
FWIW, here is what I have now - and which seems to work.
Clearly 5 seconds is better than 0 seconds, but in how many cases does
it need longer to work?
Might be better to disable it *before* stopping it, otherwise it could
get restarted while you're sleeping for the stop to take effect.
That is why I would use Pause within the script and not merely at the end.
--
David E. Ross

Pharmaceutical companies claim their drug prices are
so high because they have to recover the costs of developing
those drugs. Two questions:

1. Why is the U.S. paying the entire cost of development while
prices for the same drugs in other nations are much lower?

2. Manufacturers of generic drugs did not have those
development costs. Why are they charging so much for generics?
Zaidy036
2015-12-31 16:44:42 UTC
Permalink
Post by David E. Ross
Post by Andy Burns
Post by (PeteCresswell)
FWIW, here is what I have now - and which seems to work.
Clearly 5 seconds is better than 0 seconds, but in how many cases does
it need longer to work?
Might be better to disable it *before* stopping it, otherwise it could
get restarted while you're sleeping for the stop to take effect.
That is why I would use Pause within the script and not merely at the end.
also consider using
TIMEOUT 5 instead of CHOICE /N /T 5 /D Y

(PeteCresswell)
2015-12-31 16:44:01 UTC
Permalink
Post by Andy Burns
Might be better to disable it *before* stopping it, otherwise it could
get restarted while you're sleeping for the stop to take effect.
Point Taken....

Thanks !
--
Pete Cresswell
Loading...