python 脚本作为Windows服务启动

相关包:http://sourceforge.net/projects/pywin32/
[python]
import win32serviceutil
import win32service
import win32event
import time
class test1(win32serviceutil.ServiceFramework):
_svc_name_ = "test_python"
_svc_display_name_ = "test_python"
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(test1)
[/python]

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据