Qual è lo scopo?
se si desidera che un processo nascosto (senza finestra) funzioni in background, il modo migliore sarebbe scrivere un servizio Windows e avviarlo/arrestarlo utilizzando il solito meccanismo di servizio delle finestre. Il servizio Windows può essere facilmente scritto in python, ad es. qui è parte del mio proprio servizio (che non verrà eseguito senza alcune modifiche)
import os
import time
import traceback
import pythoncom
import win32serviceutil
import win32service
import win32event
import servicemanager
import jagteraho
class JagteRahoService (win32serviceutil.ServiceFramework):
_svc_name_ = "JagteRaho"
_svc_display_name_ = "JagteRaho (KeepAlive) Service"
_svc_description_ = "Used for keeping important services e.g. broadband connection up"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.stop = False
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.log('stopping')
self.stop = True
def log(self, msg):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,msg))
def SvcDoRun(self):
self.log('folder %s'%os.getcwd())
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
self.start()
def shouldStop(self):
return self.stop
def start(self):
try:
configFile = os.path.join(jagteraho.getAppFolder(), "jagteraho.cfg")
jagteraho.start_config(configFile, self.shouldStop)
except Exception,e:
self.log(" stopped due to eror %s [%s]" % (e, traceback.format_exc()))
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
ed è possibile installarlo da
python svc_jagteraho.py--startup auto install
ed eseguirlo da
python python svc_jagteraho.py start
sarò anche essere visto nella lista dei servizi ad es services.msc mostrerà esso e si può avviare/fermare altro si può usare da riga di comando
sc stop jagteraho
fonte
2010-02-23 16:42:18
Ti capita di conoscere il modulo corrispondente su Linux? – helplessKirk