Python代码写服务

#encoding=utf-8

import win32serviceutil 

import win32service 

import win32event 

 

class PythonService(win32serviceutil.ServiceFramework): 

    #服务名

    _svc_name_ = "PythonService"

    #服务在windows系统中显示的名称

    _svc_display_name_ = "Python Service Test"

    #服务的描述

    _svc_description_ = "This code is a Python service Test"

 

    def __init__(self, args): 

        win32serviceutil.ServiceFramework.__init__(self, args) 

        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

 

    def SvcDoRun(self):

        # 把自己的代码放到这里,就OK

        # 等待服务被停止 

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

            

    def SvcStop(self): 

        # 先告诉SCM停止这个过程 

        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 

        # 设置事件 

        win32event.SetEvent(self.hWaitStop) 

 

if __name__=='__main__': 

    win32serviceutil.HandleCommandLine(PythonService)  

    #括号里参数可以改成其他名字,但是必须与class类名一致;

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

猜你喜欢

转载自blog.csdn.net/lixiaoyu101/article/details/84567411
今日推荐