清风徐来
Michael's Blog
一个最简单的用PYTHON实现的WINDOWS服务程序 [转]

原文:http://blog.sina.com.cn/s/blog_633b6d790100g4tu.html

1、想写一个监视服务器是否运行的简单服务,网上搜到的例程不太完善,如梅劲松的许久 没有更新,而且SvcDoRun写得不完整(见http://www.chinaunix.net/jh/55/558190.html, 不知道是不是原始出处);而mail.python.org中的没有定义_svc_name_等变量(见 http://mail.python.org/pipermail/python-list/2005-December/315190.html) 2、这个实现功能很简单,就是把当前时间写入‘c:\temp\time.txt’文件,一看即知, 大家可以随意扩充。 3、用 service install 安装 service start 启动 service stop 停止 service debug 调试 service remove 删除

service.py #coding:utf-8 import win32serviceutil import win32service import win32event import win32evtlogutil import time class service(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) print ‘服务开始’ def SvcDoRun(self): import servicemanager #—————————————————— # Make entry in the event log that this service started #—————————————————— servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self.svc_name, ‘')) #————————————————————- # Set an amount of time to wait (in milliseconds) between runs #————————————————————- self.timeout=100 while 1: #——————————————————- # Wait for service stop signal, if I timeout, loop again #——————————————————- rc=win32event.WaitForSingleObject(self.hWaitStop,self.timeout) # # Check to see if self.hWaitStop happened # if rc == win32event.WAIT_OBJECT_0: # # Stop signal encountered # break else: # # Put your code here # # f=open(‘c:\temp\time.txt’,‘w’,0) f.write(time.ctime(time.time())) f.close() print ‘服务运行中’ time.sleep(1) # # Only return from SvcDoRun when you wish to stop # return

    def SvcStop(self):

#——————————————————————— # Before we do anything, tell SCM we are starting the stop process. #——————————————————————— self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) #——————————————————————— # And set my event #——————————————————————— win32event.SetEvent(self.hWaitStop) print ‘服务结束’ return if name=='main': win32serviceutil.HandleCommandLine(service)


最后修改于 2013-10-30