简体   繁体   中英

Running a Windows service on machine startup

I have a Windows service written with Python. I want to start it automatically when the machine starts.

How can I do that?

class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "Service"
_svc_display_name_ = "Myservice"

def __init__(self,args):
    win32serviceutil.ServiceFramework.__init__(self,args)
    self.hWaitStop = win32event.CreateEvent(None,0,0,None)
    #socket.setdefaulttimeout(60)

def SvcStop(self):
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    win32event.SetEvent(self.hWaitStop) 

def SvcDoRun(self):
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_,''))
    self.timeout = 120000
    self.main()              #//thread.start_new_thread(main)

def main(self):
        pass

def ctrlHandler(ctrlType):
    return True         

if __name__ == '__main__':
    win32api.SetConsoleCtrlHandler(ctrlHandler, True)
    win32serviceutil.HandleCommandLine(AppServerSvc)

Try the task scheduler:

http://support.microsoft.com/kb/308569

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM