I'm currently trying to create a Windows Service using a Python script :
import win32serviceutil
import time
import sys
import servicemanager
import win32service
import win32event
import win32service
import win32serviceutil
import time
from BIUtilities import script_execution
from abc import ABC,abstractmethod
class PythonService(win32serviceutil.ServiceFramework, ABC):
_svc_name_ = "Parseltongue"
_svc_display_name_ = "Parseltongue"
_svc_description_ = "A service to run python scripts automatically"
@classmethod
def parse_command_line(cls):
win32serviceutil.HandleCommandLine(cls)
# * DO SOMETHING BEFORE SERVICE STOPS
@abstractmethod
def stop(self):
pass
# * DO SOMETHING AT SERVICE START
@abstractmethod
def start(self):
pass
# * MAIN FUNCTIONALITY
@abstractmethod
def main(self):
pass
# * CONSTRUCTOR
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
# * CREATE EVENT FOR SERVICE TO WAIT ON
self.hWaitStop = win32event.CreateEvent(None, 0, 0,None)
# * WHEN ASKED TO STOP :
def SvcStop(self):
self.stop()
# * TELL SERVICE WE WILL BE STOPPING
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# * SET EVENT AT STOP
win32event.SetEvent(self.hWaitStop)
# * WHEN ASKED TO RUN :
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
# * SET EVENT AT START
self.start()
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
# * BEGIN LOOP THAT WAITS FOR COMMANDS
self.main()
class Parseltongue(PythonService):
_svc_name_ = "Parseltongue"
_svc_display_name_ = "Parseltongue"
_svc_description_ = "A service to run python scripts automatically"
# * SET RUN CONDITION
def start(self):
self.isrunning =True
# * OVERRIDE RUN CONDITION AT STOP
def stop(self):
self.isrunning =False
# * MAIN FUNCTIONALITY
def main(self):
while self.isrunning:
script_execution('test.py')
time.sleep(5)
def initialize():
# * FOR RUNNING OUR SCRIPT (THE SCRIPT IS THE ARG)
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(PythonService)
servicemanager.StartServiceCtrlDispatcher()
# * EXTRA COMMANDS (install, remove, etc.)
else:
win32serviceutil.HandleCommandLine(PythonService)
# * SET SCRIPT RUNNING CONDITONS
if __name__ == '__main__':
initialize()
The above script was converted to an .exe using PyInstaller in the following manner in powershell:
python -m PyInstaller --runtime-tmpdir=. --onefile --hidden-import win32timezone myscript.py
When the command to install the .exe is given, the service is installed correctly. But when I go to start the service, I get the following error:
Windows could not start the service on Local Computer. For more information, review the System Event Log. If this is a Non-Microsoft service, contact the service vendor, and refer to service specific error code 1
And when I check the System Event Log, the following error shows in the log:
The event logging service encountered an error (5) while enabling publisher {0bf2fb94-7b60-4b4d-9766-e82f658df540} to channel Microsoft-Windows-Kernel-ShimEngine/Operational. This does not affect channel operation, but does affect the ability of the publisher to raise events to the channel. One common reason for this error is that the Provider is using ETW Provider Security and has not granted enable permissions to the Event Log service identity.
My initial error with this service was the well-documented 1053 error (service can't run in timely manner). This error was fixed by copying C:\Python310\Lib\site-packages\pywin32_system32\pywintypes37.dll
to C:\Python310\Lib\site-packages\win32\pywintypes37.dll
and installing C:\Python310\Scripts\pywin32_postinstall.py
. Both are solutions suggested here: all python windows service can not start{error 1053}. This fixed my 1053 error, but now the problem outlined above is plaguing me. Any help/insight would be greatly greatly appreciated.
TYIA!