SoFunction
Updated on 2024-11-07

Python script as Windows service startup code details

Let's start by looking at the full code:

# -*- coding: cp936 -*- 
import win32serviceutil 
import win32service 
import win32event 
class test1(): 
  _svc_name_ = "test_python" 
  _svc_display_name_ = "test_python" 
  def __init__(self, args): 
    .__init__(self, args) 
     = (None, 0, 0, None) 
  def SvcStop(self): 
    # Tell SCM to stop the process first
    (win32service.SERVICE_STOP_PENDING) 
    # Setting up events
    () 
  def SvcDoRun(self): 
    # Wait for the service to be stopped
    (, ) 
if __name__=='__main__': 
  (test1)

Note here that if you need to change the filename, such as changing test1 in (test1) to your filename, and also the class needs to be the same as your filename, or else you will have problems with the service not starting.