用py2exe安装python windows服务

来源:互联网 发布:淘宝商品不符合资质 编辑:程序博客网 时间:2024/06/06 11:45
使用模块:pywin32,py2exe

#运行安装程序的bat

ProductCollectWin32ServiceSetup.bat

内容:

cd "E:\python\pydev\src\"
e:
python ProductCollectWin32ServiceSetup.py py2exe
pause

#安装成windows服务的python脚本

ProductCollectWin32ServiceSetup.py

内容:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
#@author zcwang3@gmail.com
#@version 2010-09-17 14:55
# mysetup.py
from distutils.core import setup
import py2exe

setup(service=["ProductCollectWin32Service"])

#python windows服务程序

ProductCollectWin32Service.py

内容:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
#@author zcwang3@gmail.com
#@version 2010-09-17 14:55
import win32event
import win32service
import win32serviceutil
class ProductCollectWin32Service (win32serviceutil.ServiceFramework):
    _svc_name_ = "pythonService"
    _svc_display_name_ = "pythonService"
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
    def SvcStop(self):
        # 先告诉SCM停止这个过程
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        # 设置事件
        win32event.SetEvent(self.hWaitStop)
    def SvcDoRun(self):
        # 等待服务被停止
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
if __name__=='__main__':
    win32serviceutil.HandleCommandLine(ProductCollectWin32Service )

 

ProductCollectWin32Service这个模块的文件名要和模块名保持一致

 

运行bat文件后可以在dist得到ProductCollectWin32ServiceSetup.exe

然后用windows的sc命令安装 删除 更新服务

安装命令:

sc create pythons binPath= E:\python\pydev\src\dist\ProductCollectWin32ServiceSetup.exe

 

参考下面的地址:“=”后面是必须空一格的,否则会出现错误。

http://daohao123.iteye.com/blog/568507

 

sc命令详解:

sc delete [servicename]

使用这个命令即可删除一个windows服务

描述:
         SC 是用于与服务控制管理器通信的命令行程序。
用法:
         sc <server> [command] [service name] <option1> <option2>...


         选项 <server> 的格式为 "\\ServerName "
         可以键入 "sc [command]"以获得命令的进一步帮助
         命令:
           query-----------查询服务的状态,
                           或枚举服务类型的状态。
           queryex---------查询服务的扩展状态,
                           或枚举服务类型的状态。
           start-----------启动服务。
           pause-----------发送 PAUSE 控制请求到服务。
           interrogate-----发送 INTERROGATE 控制请求到服务。
           continue--------发送 CONTINUE 控制请求到服务。
           stop------------发送 STOP 请求到服务。
           config----------(永久地)更改服务的配置。
           description-----更改服务的描述。
           failure---------更改服务失败时所进行的操作。
           qc--------------查询服务的配置信息。
           qdescription----查询服务的描述。
           qfailure--------查询失败服务所进行的操作。
           delete----------(从注册表)删除服务。
           create----------创建服务(将其添加到注册表)。
           control---------发送控制到服务。
           sdshow----------显示服务的安全描述符。
           sdset-----------设置服务的安全描述符。
           GetDisplayName--获取服务的 DisplayName。
           GetKeyName------获取服务的 ServiceKeyName。
           EnumDepend------枚举服务的依存关系。

         下列命令不查询服务名称:
         sc <server> <command> <option>
           boot------------(ok | bad) 表明是否将上一次启动保存为
                           最后所知的好的启动配置
           Lock------------锁定服务数据库
           QueryLock-------查询 SCManager 数据库的 LockStatus

引自:http://desert3.iteye.com/blog/765968


原创粉丝点击