2010-12-27 9 views
6

Sto scrivendo un servizio python win32 di seguito sono snippet del mio codice quando compilo il servizio funziona, ma ho bisogno di andare a services.msc e avviarlo manualmente.Python win32 service starting automaticlly

Esiste un'opzione quando si installa il servizio tramite: myservice.exe, l'installazione verrà avviata automaticamente?

seguito sono frammento di mio codice:

import win32serviceutil 
import win32service 
import win32event 

class SmallestPythonService(win32serviceutil.ServiceFramework): 
    _svc_name_ = "ser_name" 
    _svc_display_name_ = "ser_descryption" 
    #_svc_description_='ddd' 
    def __init__(self, args): 

     win32serviceutil.ServiceFramework.__init__(self, args) 
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) 

    def SvcStop(self): 

     self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
     win32event.SetEvent(self.hWaitStop) 

    def SvcDoRun(self): 

     win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) 

if __name__=='__main__': 

    win32serviceutil.HandleCommandLine(SmallestPythonService) 

risposta

1

È possibile utilizzare sc.exe con il comando create.

sc create MyPyService binPath= "C:\myservice.exe" DisplayName= "Some Python Service" 

più su questo a Microsoft KB251192.

win32serviceutil ha anche una funzione InstallService() che potrebbe essere in grado di utilizzare.

2

Vorrei dare un'occhiata a questo ActiveState recipe. È un wrapper attorno al win32serviceutil che mostra come avviare automaticamente il servizio.

6

Utilizzare myservice.exe --startup=auto install per installare il servizio e impostarlo per essere avviato automaticamente.

+0

soluzione Nativ è la soluzione migliore! – enthus1ast

0

@Maciejg non funziona per me, qui la soluzione per avviare automaticamente il mio servizio costruito con py2exe:

myservice.exe -auto -install