python 做成windows service 不完全版
By:Roy.LiuLast updated:2012-04-06
前面有提到过python 在ubuntu 配置为启动服务,只需要在rc.local 中配置就好。在WINDOWS中呢,需要借助于PYWIN32扩展模块,py2exe,来完成。
1.先写服务类 iNetServerService.py
准备一个 start.bat文件:
然后执行 python iNetServerService.py install
就可以了。
问题:可以在windows服务里配置程自动启动,但停止服务,并没有停止,原因在于调用的BAT文件,停止服务只会停止cmd,不会停止python. 所以更好的办法不是调用BAT文件,而是启动EXE文件。以后有时间再测试。
1.先写服务类 iNetServerService.py
# -*- coding:utf-8 -*-
'''
Created on 2012-3-15
@author: Administrator
'''
import sys
import os
packagepath = os.path.abspath('..')
if packagepath:
os.sys.path.append(packagepath)
else:
print 'can not set sys path.'
sys.exit(1)
#init sys path end.
# Win32 python extensions modules
import win32serviceutil
import win32service
import win32event
import win32api
import win32process
import servicemanager
import subprocess
import thread
class iNetServerService(win32serviceutil.ServiceFramework):
# required info
_svc_name_ = "iNetServerServer-1.0"
_svc_display_name_ = "iNet Server 1.0"
# optionnal info
_svc_description_ = "iNet Server 1.0 Service"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
# Create an event which we will use to wait on.
# The "service stop" request will set this event.
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
# a reference to the server's process
self.terpprocess = None
# info if the service terminates correctly or if the server crashed
self.stopping = False
def SvcStop(self):
# Before we do anything, tell the SCM we are starting the stop process.
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# stop the running TERP Server: say it's a normal exit
servicemanager.LogInfoMsg('close process pid:'+str(self.terpprocess.pid))
win32api.TerminateProcess(int(self.terpprocess._handle), -1)
servicemanager.LogInfoMsg("iNet Server stopped correctly")
# And set my event.
win32event.SetEvent(self.hWaitStop)
def StartiNet(self):
# The server finds now its configuration automatically on Windows
# We start the iNet Server as an independent process, but we keep its handle
# The server's binary must be one directory above the service's binary (when py2exe'd the python libraries shouldn' mix)
service_dir = os.path.dirname(sys.argv[0])
server_dir = os.path.split(service_dir)[0]
server_path = os.path.join(server_dir, 'inetgarment', 'start.bat')
self.terpprocess = subprocess.Popen([server_path], cwd=server_dir, creationflags=win32process.Create_NO_WINDOW)
servicemanager.LogInfoMsg(server_path+' and pid is:'+str(self.terpprocess.pid))
def StartControl(self,ws):
# this listens to the Service Manager's events
win32event.WaitForSingleObject(ws, win32event.INFINITE)
self.stopping = True
def SvcDoRun(self):
# Start OpenERP Server itself
self.StartiNet()
# start the loop waiting for the Service Manager's stop signal
thread.start_new_thread(self.StartControl, (self.hWaitStop,))
# Log a info message that the server is running
servicemanager.LogInfoMsg("iNet Server up and running")
# verification if the server is really running, else quit with an error
self.terpprocess.wait()
if not self.stopping:
sys.exit("iNet Server check: server not running, check the logfile for more info")
if __name__=='__main__':
win32serviceutil.HandleCommandLine(iNetServerService)
准备一个 start.bat文件:
cd \ cd C:\iNet\inetgarment python C:\iNet\inetgarment\serversocket.py
然后执行 python iNetServerService.py install
就可以了。
问题:可以在windows服务里配置程自动启动,但停止服务,并没有停止,原因在于调用的BAT文件,停止服务只会停止cmd,不会停止python. 所以更好的办法不是调用BAT文件,而是启动EXE文件。以后有时间再测试。
From:一号门
Previous:在windows下以指定大小创建文件

COMMENTS