Stopping Windows Service

Discussion in 'Python' started by Paul Metzger, Mar 1, 2006.

  1. Paul Metzger

    Paul Metzger Guest

    On Wed, 2006-03-01 at 12:41 -0800, D wrote:
    > I have a simple file server utility that I wish to configure as a
    > Windows service - using the examples of the Python Win32 book, I
    > configured a class for the service, along with the main class functions
    > __init__, SvcStop, and SvcDoRun (which contains my server code). After
    > registering the service, I am able to start it with no problems.
    > However, it never stops correctly (net stop returns "service could not
    > be stopped") and service is left in a Stopping state. My gut tells me
    > there's something additional I need to include in the SvcDoRun function
    > in order to have it shutdown gracefully, but I'm not sure what. Also,
    > if that is the case, where should I place the statements within the
    > function (i.e. right after the listen() statement?)? Thanks as
    > always..

    At the risk of sounding like a geek, first problem I see is WIndows:)
    Sorry couldn't help that one:) Seriously though, I would suggest that it
    sounds like something is not letting the process go, which would be par
    for the windows course. If you will post the snippet of code, I'm sure
    we could help you a little more specifically.
     
    Paul Metzger, Mar 1, 2006
    #1
    1. Advertising

  2. Paul Metzger

    D Guest

    I have a simple file server utility that I wish to configure as a
    Windows service - using the examples of the Python Win32 book, I
    configured a class for the service, along with the main class functions
    __init__, SvcStop, and SvcDoRun (which contains my server code). After
    registering the service, I am able to start it with no problems.
    However, it never stops correctly (net stop returns "service could not
    be stopped") and service is left in a Stopping state. My gut tells me
    there's something additional I need to include in the SvcDoRun function
    in order to have it shutdown gracefully, but I'm not sure what. Also,
    if that is the case, where should I place the statements within the
    function (i.e. right after the listen() statement?)? Thanks as
    always..

    Doug
     
    D, Mar 1, 2006
    #2
    1. Advertising

  3. Paul Metzger

    Larry Bates Guest

    D wrote:
    > I have a simple file server utility that I wish to configure as a
    > Windows service - using the examples of the Python Win32 book, I
    > configured a class for the service, along with the main class functions
    > __init__, SvcStop, and SvcDoRun (which contains my server code). After
    > registering the service, I am able to start it with no problems.
    > However, it never stops correctly (net stop returns "service could not
    > be stopped") and service is left in a Stopping state. My gut tells me
    > there's something additional I need to include in the SvcDoRun function
    > in order to have it shutdown gracefully, but I'm not sure what. Also,
    > if that is the case, where should I place the statements within the
    > function (i.e. right after the listen() statement?)? Thanks as
    > always..
    >
    > Doug
    >

    Stopping is done by having SvcStop set the event flag so that the
    next time through your loop in SvcDoRun you see that the stop has
    been issued and exit your while loop. Here is some skeleton code
    that I stripped from a working service. Hope it helps. Note: I
    copied most of this from Mark Hammond and Andy Robinson's excellent
    book Python Programming on Win32 book.

    -Larry Bates

    import win32serviceutil
    import win32service
    import win32event
    import win32evtlogutil

    class testService(win32serviceutil.ServiceFramework):

    def __init__(self, args):
    win32serviceutil.ServiceFramework.__init__(self, args)
    #---------------------------------------------------------------------
    # Create an event which we will use to wait on.
    # The "service stop" request will set this event.
    #---------------------------------------------------------------------
    self.hWaitStop=win32event.CreateEvent(None, 0, 0, None)
    return

    def SvcStop(self):
    #---------------------------------------------------------------------
    # Before we do anything, tell SCM we are starting the stop process.
    #---------------------------------------------------------------------
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    #---------------------------------------------------------------------
    # And set my event
    #---------------------------------------------------------------------
    win32event.SetEvent(self.hWaitStop)
    return

    def SvcDoRun(self):
    import servicemanager
    #---------------------------------------------------------------------
    # Make entry in the event log that this service started
    #---------------------------------------------------------------------
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
    servicemanager.PYS_SERVICE_STARTED,
    (self._svc_name_, ''))

    #---------------------------------------------------------------------
    # Wait 10000 milliseconds (10 seconds)
    #---------------------------------------------------------------------
    self.timeout=10000
    #-----------------------------------------------------------------
    # Wait for service stop signal, if I timeout, loop again
    #-----------------------------------------------------------------
    rc=win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
    #
    # Check to see if self.hWaitStop happened
    #
    if rc == win32event.WAIT_OBJECT_0:
    #
    # Stop signal encountered
    #
    break

    #--End while--
    #---------------------------------------------------------------------
    # Log stopped message to EventLog
    #---------------------------------------------------------------------
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
    servicemanager.PYS_SERVICE_STOPPED,
    (self._svc_name_, ''))

    return
     
    Larry Bates, Mar 1, 2006
    #3
  4. Paul Metzger

    D Guest

    Thanks guys. Larry - I inserted the rc lines right after I do the
    listen() and the service is not functioning properly (service is
    terminating). Where am I going wrong here?

    Doug
     
    D, Mar 1, 2006
    #4
  5. Paul Metzger

    D Guest

    I must be missing something..in my SvcDoRun function, first line is
    "self.timeout=10000" , followed by my "while 1:" loop which listens for
    the connection, processes the data, etc. Where should I place the
    "rc=win32event.." and "if rc" lines? I have a feeling I'm pretty close
    here.. :) Thanks very much.

    Doug
     
    D, Mar 2, 2006
    #5
  6. Paul Metzger

    D Guest

    Sorry for the many messages, but I've been looking into it further -
    since I have the server services listening and waiting for incoming
    connections, how would I interrupt it to let the program know that a
    stop request has been issued? For example, if I put the stop check
    before it listens, I would think it'd have to wait until it loops back
    around after the next client connects. If I put it after the listen, a
    client would have to connect in order for it to run the check..?
     
    D, Mar 2, 2006
    #6
  7. Paul Metzger

    Larry Bates Guest

    D wrote:
    > Sorry for the many messages, but I've been looking into it further -
    > since I have the server services listening and waiting for incoming
    > connections, how would I interrupt it to let the program know that a
    > stop request has been issued? For example, if I put the stop check
    > before it listens, I would think it'd have to wait until it loops back
    > around after the next client connects. If I put it after the listen, a
    > client would have to connect in order for it to run the check..?
    >

    You are correct. Services must be "good neighbors". They
    do some work and go to sleep for some period of time, then wake
    up and loop. The stop signal comes while they are asleep.
    When they wake up, they exit the infinite loop. If you can't
    implement a listen-with-timeout it is going to be hard to make
    this into a service.

    -Larry Bates
     
    Larry Bates, Mar 3, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joe
    Replies:
    1
    Views:
    2,716
    Alan Pretre
    Jul 10, 2003
  2. ThunderMusic

    Prevent service from stopping

    ThunderMusic, Jan 11, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    4,798
  3. DK
    Replies:
    6
    Views:
    2,529
    Peter Hansen
    Aug 18, 2005
  4. Gheorghe Postelnicu
    Replies:
    0
    Views:
    771
    Gheorghe Postelnicu
    Apr 30, 2007
  5. teknokrat

    Stopping service on windows with signals

    teknokrat, Nov 14, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    115
    teknokrat
    Nov 14, 2003
Loading...

Share This Page