• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

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
0
drrman
Asked:
drrman
2 Solutions
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
NET STOP "Service name"

http://support.microsoft.com/kb/236166

- Rancy
0
 
drrmanAuthor Commented:
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
0
 
kevinhsiehCommented:
You can use monitoring software such as IPSENTRY that can monitor the IP address you want. It can then stop or start your service according to the availability of that remote IP. Since you are only monitoring a single IP, it is free for your use I believe, as long as the service controller add-in is free as well.

ipsentry.com
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
drrmanAuthor Commented:
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
0
 
redmondbCommented:
Hi, drrman.

Please see the batch file below. When the link drops the file terminates - to leave it running just drop line 39 ("Goto EOJ")
@echo off

:Loop

    Ping www.google.com 1>Nul 2>Nul
    
    If ErrorLevel 1 (set xStatus=Fail) Else (set xStatus=Success)
    
    if _%xstatus%_==_Success_ 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
    
    echo.
    Choice /C wc /N /T 300 /D W /M "Sleeping for 5 Minutes. W=Wake. C=End run.
    If ErrorLevel 2 Goto EOJ

Goto Loop

:EOJ
set xStatus=
pause

Open in new window

Regards,
Brian.
0
 
redmondbCommented:
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.
0
 
drrmanAuthor Commented:
actually if i could ping every second and then after 300 consecutive fails that would be perfect... Thank you a ton!
0
 
redmondbCommented:
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.
@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

Open in new window

Regards,
Brian.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now