Solved

Force Stop Windows Service After Loss of Network Connectivity

Posted on 2013-02-05
8
425 Views
Last Modified: 2013-02-06
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
Comment
Question by:drrman
8 Comments
 
LVL 52

Expert Comment

by:Manpreet SIngh Khatra
ID: 38860206
NET STOP "Service name"

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

- Rancy
0
 

Author Comment

by:drrman
ID: 38860272
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
 
LVL 42

Assisted Solution

by:kevinhsieh
kevinhsieh earned 150 total points
ID: 38860419
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:drrman
ID: 38860439
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
 
LVL 26

Accepted Solution

by:
redmondb earned 350 total points
ID: 38860482
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
 
LVL 26

Expert Comment

by:redmondb
ID: 38860797
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
 

Author Comment

by:drrman
ID: 38860903
actually if i could ping every second and then after 300 consecutive fails that would be perfect... Thank you a ton!
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38861606
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now