python实现windows Service服务程序
python实现windows Service服务程序
win32serviceutil.ServiceFramework是封装得很好的Windows服务框架,本文通过继承它来实现。
- 通过SvcDoRun方法,实现服务启动,运行服务内的业务代码。
- 通过SvcStop方法,停止服务。
WinPollManager.py代码如下:
import win32serviceutil
import win32service
import win32event
import winerror
import servicemanager
import time
import sys
import os class WinPollManager(win32serviceutil.ServiceFramework):
"""
#1.安装服务
python WinPollManager.py install #2.让服务自动启动
python WinPollManager.py --startup auto install #3.启动服务
python WinPollManager.py start #4.重启服务
python WinPollManager.py restart #5.停止服务
python WinPollManager.py stop #6.删除/卸载服务
python WinPollManager.py remove
""" _svc_name_ = "py_agent_poll_manager" # 服务名
_svc_display_name_ = "py_agent_poll_manager" # 服务在windows系统中显示的名称
_svc_description_ = "python windows monitor agent" # 服务的描述 def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.isAlive = True
self._poll_intvl = 30 def SvcDoRun(self):
while self.isAlive:
print 'monitor testing'
time.sleep(self._poll_intvl) def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.isAlive = False if __name__ == '__main__':
if len(sys.argv) == 1:
try:
evtsrc_dll = os.path.abspath(servicemanager.__file__)
servicemanager.PrepareToHostSingle(WinPollManager)
servicemanager.Initialize('WinPollManager', evtsrc_dll)
servicemanager.StartServiceCtrlDispatcher()
except win32service.error, details:
if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
win32serviceutil.usage()
else:
win32serviceutil.HandleCommandLine(WinPollManager) # 括号里参数可以改成其他名字,但是必须与class类名一致;
打包exe文件
# -*- coding: utf-8 -*- """
pip install pyinstaller
pyinstaller -F -w WinPollManager.py
"""
from PyInstaller.__main__ import run if __name__ == '__main__':
params = ['WinPollManager.py', '-F', '-c', '--icon=favicon.ico']
run(params)
打包成功后在dist目录下生成exe文件

执行方式
- 安装服务 WinPollManager.exe install
- 服务自动启动 WinPollManager.exe --startup auto install
- 启动服务 WinPollManager.exe start
- 重启服务 WinPollManager.exe restart
- 停止服务 WinPollManager.exe stop
- 删除/卸载服务 WinPollManager.exe remove
参考文章:
http://zhangweide.cn/archive/2013/windows-service-example-using-pyinstaller.html
http://www.cnblogs.com/dcb3688/p/4496934.html
http://blog.csdn.net/dysj4099/article/details/21896085
python实现windows Service服务程序的更多相关文章
- 使用Python写Windows Service服务程序
1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...
- Python 写Windows Service服务程序
1.需求 为什么要开发一个windows服务呢?之前做一个程序,必须要读取指定目录文件License, 因为其他程序也在读取这指定目录的License文件,且License不同时会修改License的 ...
- C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计
C#Windows Service服务程序的安装/卸载.启动/停止 桌面客户端管理程序设计 关于Windows Service程序的安装与卸载如果每次使用命令行操作,那简直要奔溃了,太麻烦而且还容易出 ...
- Python3 写Windows Service服务程序
用Python开发Windows Service,用Python来做这个事情必须要借助第三方模块pywin32,下载路径:https://pypi.org/project/pywin32/#files ...
- 使用Advanced Installer 13.1打包发布 Windows Service服务程序
原文: 使用Advanced Installer 13.1打包发布 Windows Service服务程序 项目中需要用到一个定时推送案件状态的需求,本人小菜一只,在同事建议下要写成一个windows ...
- C#Windows Service程序的创建安装与卸载
C#Windows Service程序的创建安装与卸载 一.开发环境 操作系统:Windows7x64 sp1 专业版 开发环境:Visual studio 2013 编程语言:C# .NET版本: ...
- C#写Windows Service(windows服务程序)
背景: 要学习使用一个新东西,我们必须知道他是个什么东西.对于我们此次研究的windows服务来说,他又是个什么东西,其实也没有什么高深的了. windows service概述: 一个 ...
- C# 编写Windows Service(windows服务程序)【转载】
[转]http://www.cnblogs.com/bluestorm/p/3510398.html Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成 ...
- 让自己的C++程序(非服务程序)运行为一个windows service
因为项目的一些变化和原因,需要把数据处理的一个后台程序创建为一个windows服务,运行以下命令能创建成功: sc create "MyApp Service Name" binP ...
随机推荐
- Vxlan抓包
实验目的:验证Openstack vxlan组网模式验证虚拟机数据是否通过物理网卡流出 一. 同网段不同主机间虚拟机通讯 (同网段通讯直接通过物理机隧道口链接对端物理机隧道口,不需要通过网络节点): ...
- Python中浅拷贝和深拷贝的区别总结与理解
单层浅拷贝 import copy a = 1 # 不可变数据类型 copy_a = copy.copy(a) print(id(a),id(copy_a)) # 内存地址相同 a = [1,2] # ...
- SICP读书笔记 1.1
SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...
- 1086. Tree Traversals Again (25)-树的遍历
题意:用栈的push.pop操作给出一棵二叉树的中序遍历顺序,求这棵二叉树的后序遍历. 需要一个堆结构s,一个child变量(表示该节点是其父亲节点的左孩子还是右孩子),父亲节点fa对于push v操 ...
- 12.19daily_scrum
本阶段已经过去一半,在周末短暂的休息后,我们将继续完成后续的任务,今日工作情况如下: 小组成员 今日任务 工作时间 李睿琦 网络数据传输应用设计 2 左少辉 主界面功能测试 3 程刚 界面优化学习 4 ...
- 11慕课网《进击Node.js基础(一)》Buffer和Stream
Buffer 用来保存原始数据 (logo.png) 以下代码读取logo.png为buffer类型 然后将buffer转化为string,新建png 可以将字符串配置: data:image/png ...
- 团队冲刺随笔合集—Beta阶段
第一篇:http://www.cnblogs.com/Team-Blog/p/9049271.html 第二篇:https://www.cnblogs.com/Team-Blog/p/9064478. ...
- Mac OS X使用简介
一.OS X 版本以大型猫科动物命名 10.0 猎豹(Cheetah) 10.1 美洲狮(Puma) 10.2 美洲虎(Jaguar) 10.3 黑豹(Panther) 10.4 ...
- 链表的C/C++实现
一个链表实现,函数声明放在 list.h 头文件汇总,函数定义放在list.cpp 中,main.cpp 用来测试各个函数. 1.文件list.h // list.h #ifndef __LIST_H ...
- [转帖知乎]5G 网络和 4G 网络有什么区别?
5G 网络和 4G 网络有什么区别? 先放上一篇有史以来最强的5G科普: 一个简单且神奇的公式 今天的故事,从一个公式开始讲起. 这是一个既简单又神奇的公式.说它简单,是因为它一共只有3个字母.而说它 ...