drrman
asked on
Force Stop Windows Service After Loss of Network Connectivity
I need a process/script to stop a windows service after loss of network connectivity. For example I have a service running called "myservice"
Scenario
1.) "myservice" is running as an AD user using "logon as a service" local security rights.
2.) "Myservice" is set to manual startup
2.) Network cable is unplugged from machine where "myservice" had authenticated and started successfully
3.) after a specified amount of time with loss of connectivity, lets say 5 minutes, I need "myservice" to be stopped
Ultimate goal would be a method to ping a specified public IP address and upon loss of response for 5 minutes, "myservice" gets killed/stopped
Scenario
1.) "myservice" is running as an AD user using "logon as a service" local security rights.
2.) "Myservice" is set to manual startup
2.) Network cable is unplugged from machine where "myservice" had authenticated and started successfully
3.) after a specified amount of time with loss of connectivity, lets say 5 minutes, I need "myservice" to be stopped
Ultimate goal would be a method to ping a specified public IP address and upon loss of response for 5 minutes, "myservice" gets killed/stopped
ASKER
i am well aware of how to stop a service, i need this to have a dependency of loss of network connectivity for specified amount of time before happening
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great information Kevin, this may be a perfect short term fix until I can get our developers to create something. Going to test later this evening
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks, drrman.
Please note that if the internet connection goes down 1 second before the Ping then the Service will be stopped. Is that OK? An alternative would be to ping every second, say, and only close the Service when there had been 300 consecutive fails. Please let me know if you want me to implement this.
Regards,
Brian.
Please note that if the internet connection goes down 1 second before the Ping then the Service will be stopped. Is that OK? An alternative would be to ping every second, say, and only close the Service when there had been 300 consecutive fails. Please let me know if you want me to implement this.
Regards,
Brian.
ASKER
actually if i could ping every second and then after 300 consecutive fails that would be perfect... Thank you a ton!
drrman,
I'm glad you said that, please see below! You'll need to check out the timings yourself. For example, each successful iteration takes one second plus the time for the ping - not just a second. And the timings are sometimes a lot worse for failed pings, so you might want to change the value in line 15 from 300 to something lower.
Brian.
I'm glad you said that, please see below! You'll need to check out the timings yourself. For example, each successful iteration takes one second plus the time for the ping - not just a second. And the timings are sometimes a lot worse for failed pings, so you might want to change the value in line 15 from 300 to something lower.
@echo off
set xCount=0
:Loop
Ping www.google.com -n 1 1>Nul 2>Nul
If ErrorLevel 1 (set xStatus=Fail) Else (set xStatus=Success)
if _%xstatus%_==_Success_ set xCount=0
if not _%xstatus%_==_Success_ set /a xcount=%xcount+1
if %xCount% LSS 300 Goto OK
echo Stopping MyService...
Net Stop MyService 1>Nul 2>Nul
if errorlevel 0 if not errorlevel 1 echo Service stopped.
if errorlevel 2 if not errorlevel 3 echo Error code 2 - Not Supported. (Service may simply not be running or you may not have a sufficient Privilege.)
if errorlevel 3 if not errorlevel 4 echo Error code 3 - Access Denied
if errorlevel 4 if not errorlevel 5 echo Error code 4 - Dependent Services Running
if errorlevel 5 if not errorlevel 6 echo Error code 5 - Invalid Service Control
if errorlevel 6 if not errorlevel 7 echo Error code 6 - Service Cannot Accept Control
if errorlevel 7 if not errorlevel 8 echo Error code 7 - Service Not Active
if errorlevel 8 if not errorlevel 9 echo Error code 8 - Service Request Timeout
if errorlevel 9 if not errorlevel 10 echo Error code 9 - Unknown Failure
if errorlevel 10 if not errorlevel 11 echo Error code 10 - Path Not Found
if errorlevel 11 if not errorlevel 12 echo Error code 11 - Service Already Running
if errorlevel 12 if not errorlevel 13 echo Error code 12 - Service Database Locked
if errorlevel 13 if not errorlevel 14 echo Error code 13 - Service Dependency Deleted
if errorlevel 14 if not errorlevel 15 echo Error code 14 - Service Dependency Failure
if errorlevel 15 if not errorlevel 16 echo Error code 15 - Service Disabled
if errorlevel 16 if not errorlevel 17 echo Error code 16 - Service Logon Failure
if errorlevel 17 if not errorlevel 18 echo Error code 17 - Service Marked For Deletion
if errorlevel 18 if not errorlevel 19 echo Error code 18 - Service No Thread
if errorlevel 19 if not errorlevel 20 echo Error code 19 - Status Circular Dependency
if errorlevel 20 if not errorlevel 21 echo Error code 20 - Status Duplicate Name
if errorlevel 21 if not errorlevel 22 echo Error code 21 - Status Invalid Name
if errorlevel 22 if not errorlevel 23 echo Error code 22 - Status Invalid Parameter
if errorlevel 23 if not errorlevel 24 echo Error code 23 - Status Invalid Service Account
if errorlevel 24 if not errorlevel 25 echo Error code 24 - Status Service Exists
if errorlevel 25 if not errorlevel 26 echo Error code 25 - Service Already Paused
if errorlevel 26 echo Unknown error code.
goto EOJ
:OK
Choice /C wc /N /T 1 /D W /M "Sleeping for 1 second. W=Wake. C=End run. [%xCount%]
If ErrorLevel 2 Goto EOJ
Goto Loop
:EOJ
set xStatus=
pause
Regards,Brian.
http://support.microsoft.com/kb/236166
- Rancy