예제 #1
0
    def Stop(self):
        """
        Stops the service.
        """
        self.GetServiceHandle()
        # Make sure the service is not already stopped.
        ssStatus = self.GetStatus()
        if ssStatus.dwCurrentState == SERVICE_STOPPED:
            return
        # If a stop is pending, wait for it.
        dwStartTime = GetTickCount()
        dwTimeout = 30000
        while ssStatus.dwCurrentState == SERVICE_STOP_PENDING:
            # Do not wait longer than the wait hint. A good interval is
            # one-tenth of the wait hint but not less than 1 second
            # and not more than 10 seconds.
            Sleep(min(max(1000, ssStatus.dwWaitHint / 10), 10000))

            ssStatus = self.GetStatus()

            if ssStatus.dwCurrentState == SERVICE_STOPPED:
                return
            if GetTickCount() - dwStartTime > dwTimeout:
                raise TimeOutError()
        # If the service is running, dependencies must be stopped first.
        #self.StopDependentServices()

        # Send a stop code to the service.
        if not ControlService(
                self.schService,
                SERVICE_CONTROL_STOP,
                cast(byref(ssStatus), LPSERVICE_STATUS)
        ):
            raise WinError()

        # Wait for the service to stop.
        while ssStatus.dwCurrentState != SERVICE_STOPPED:
            Sleep(ssStatus.dwWaitHint)
            ssStatus = self.GetStatus()
            if ssStatus.dwCurrentState == SERVICE_STOPPED:
                break
            if GetTickCount() - dwStartTime > dwTimeout:
                raise TimeOutError()
예제 #2
0
    def Start(self):
        """
        Starts the service.
        """
        self.GetServiceHandle()
        # Check the status in case the service is not stopped.
        ssStatus = self.GetStatus()
        # Check if the service is already running. It would be possible to stop
        # the service here, but for simplicity this example just returns.
        if (
            ssStatus.dwCurrentState != SERVICE_STOPPED and
            ssStatus.dwCurrentState != SERVICE_STOP_PENDING
        ):
            return

        # Save the tick count and initial checkpoint.
        dwStartTickCount = GetTickCount()
        dwOldCheckPoint = ssStatus.dwCheckPoint
        # Wait for the service to stop before attempting to start it.
        while ssStatus.dwCurrentState == SERVICE_STOP_PENDING:
            # Do not wait longer than the wait hint. A good interval is
            # one-tenth of the wait hint but not less than 1 second
            # and not more than 10 seconds.
            Sleep(min(max(1000, ssStatus.dwWaitHint / 10), 10000))

            # Check the status until the service is no longer stop pending.
            ssStatus = self.GetStatus()

            if ssStatus.dwCheckPoint > dwOldCheckPoint:
                # Continue to wait and check.
                dwStartTickCount = GetTickCount()
                dwOldCheckPoint = ssStatus.dwCheckPoint
            else:
                if GetTickCount() - dwStartTickCount > ssStatus.dwWaitHint:
                    raise TimeOutError()
        # Attempt to start the service.
        if not StartService(
            self.schService,  # handle to service
            0,                # number of arguments
            None              # no arguments
        ):
            raise WinError()
        #print("Service start pending...")
        # Check the status until the service is no longer start pending.
        ssStatus = self.GetStatus()
        # Save the tick count and initial checkpoint.
        dwStartTickCount = GetTickCount()
        dwOldCheckPoint = ssStatus.dwCheckPoint
        while ssStatus.dwCurrentState == SERVICE_START_PENDING:
            # Do not wait longer than the wait hint. A good interval is
            # one-tenth the wait hint, but no less than 1 second and no
            # more than 10 seconds.
            Sleep(min(max(1000, ssStatus.dwWaitHint / 10), 10000))

            # Check the status again.
            ssStatus = self.GetStatus()

            if ssStatus.dwCheckPoint > dwOldCheckPoint:
                # Continue to wait and check.
                dwStartTickCount = GetTickCount()
                dwOldCheckPoint = ssStatus.dwCheckPoint
            else:
                if GetTickCount() - dwStartTickCount > ssStatus.dwWaitHint:
                    # No progress made within the wait hint.
                    break
        # Determine whether the service is running.

        if ssStatus.dwCurrentState == SERVICE_RUNNING:
            return True
        else:
            print "Service not started."
            print "  Current State:", ssStatus.dwCurrentState
            print "  Exit Code:", ssStatus.dwWin32ExitCode
            print "  Check Point:", ssStatus.dwCheckPoint
            print "  Wait Hint:", ssStatus.dwWaitHint
            raise Exception("Service not started.")