问题
I know there are similar topics to this on StackOverflow, but none of them have the same issue as me. Most of the questions are asking how to start a service from Python. I have a .bat file that is creating a service, and using a PythonFile.exe that I created using py2exe. I am getting the error "Error starting service. The service did not respond to the start or control request in a timely fashion". The service does not run, but I see the executable in the ProcessManager processes.
Are there specific requirements for executables to be eligible as a service? My executable is just a TCP server that Sleeps (using a mutex) until the mutex is unlocked.
My .bat file...
net stop "FabulousAndOutrageousOinkers"
%SYSTEMROOT%\system32\sc.exe delete "FabulousAndOutrageousOinkers"
%SYSTEMROOT%\system32\sc.exe create "FabulousAndOutrageousOinkers" binPath= "%CD%\FabulousAndOutrageousOinkers.exe" start= auto
net start "FabulousAndOutrageousOinkers"
回答1:
I ended up finding an answer to my question. There are in fact requirements to be a service. Most scripts or programs that become services have a wrapper layer above the code to manage handling these requirements. This wrapper ends up calling the developer's code, and signaling the windows service with different types of statuses. Start, Stop, etc...
import win32service
import win32serviceutil
import win32event
class Service(win32serviceutil.ServiceFramework):
# you can NET START/STOP the service by the following name
_svc_name_ = "FabulousAndOutrageousOinkers"
# this text shows up as the service name in the Service
# Control Manager (SCM)
_svc_display_name_ = "Fabulous And Outrageous Oinkers"
# this text shows up as the description in the SCM
_svc_description_ = "Truly truly outrageous"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self,args)
# create an event to listen for stop requests on
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
# core logic of the service
def SvcDoRun(self):
import servicemanager
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
self.start()
rc = None
# if the stop event hasn't been fired keep looping
while rc != win32event.WAIT_OBJECT_0:
# block for 5 seconds and listen for a stop event
rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)
self.stop()
# called when we're being shut down
def SvcStop(self):
# tell the SCM we're shutting down
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# fire the stop event
win32event.SetEvent(self.hWaitStop)
def start(self):
try:
file_path = "FabulousAndOutrageousOinkers.exe"
execfile(file_path) #Execute the script
except:
pass
def stop(self):
pass
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(Service)
I found this template from http://www.chrisumbel.com/article/windows_services_in_python. This code still has some issues because I get the error "Error starting service: The service did not respond to the start or control request in a timely fashion", but it still answers my question. There are in fact requirements for an executable to become a Windows Service.
来源:https://stackoverflow.com/questions/36484101/python-executable-as-windows-service