Stopping Windows Service

Larry Bates larry.bates at websafe.com
Wed Mar 1 16:14:04 EST 2006


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





More information about the Python-list mailing list