#3.服务操作命令#1.安装服务#python PythonService.py install#2.让服务自动启动#python PythonService.py --startup auto install#3.启动服务#python PythonService.py start#4.重启服务#python PythonService.py restart#5.停止服务#python PythonService.py stop#6.删除/卸载服务##python PythonService.py remove#4.使用pyinstaller打包exe##pyinstaller.exe -F -c winService.py#encoding=utf-8import win32serviceutilimport win32timezoneimport win32serviceimport win32eventimport os,sysimport loggingimport inspectimport servicemanagerclass PythonService(win32serviceutil.ServiceFramework):_svc_name_ = "PythonService" #服务名_svc_display_name_ = "Python Service Test" #服务在windows系统中显示的名称_svc_description_ = "This is a python service test code " #服务的描述def __init__(self, args):win32serviceutil.ServiceFramework.__init__(self, args)self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)self.logger = self._getLogger()self.run = Truedef _getLogger(self):logger = logging.getLogger('[PythonService]')this_file = inspect.getfile(inspect.currentframe())dirpath = os.path.abspath(os.path.dirname(this_file))handler = logging.FileHandler(os.path.join(dirpath, "service.log"))formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')handler.setFormatter(formatter)logger.addHandler(handler)logger.setLevel(logging.INFO)return loggerdef SvcDoRun(self):import timeself.logger.info("service is run....")while self.run:self.logger.info("I am runing....")time.sleep(2)def SvcStop(self):self.logger.info("service is stop....")self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)win32event.SetEvent(self.hWaitStop)self.run = Falseif __name__=='__main__':if len(sys.argv) == 1:try:evtsrc_dll = os.path.abspath(servicemanager.__file__)servicemanager.PrepareToHostSingle(PythonService)servicemanager.Initialize('PythonService', evtsrc_dll)servicemanager.StartServiceCtrlDispatcher()except win32service.error.details:if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:win32serviceutil.usage()else:win32serviceutil.HandleCommandLine(PythonService)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。