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 osclass 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 = Trueself._poll_intvl = 30def 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 = Falseif __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 runif __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程序_【Python学习】Python 写Windows Service服务程序
如下遇到自己编写的服务无法启动 需要添加环境变量(标红的) C:\Python27\Scripts;C:\Python27\;C:\Python27\chromedriver.exe;C:\Pytho ...
- Windows Service服务程序的原理及实现(0)服务主函数 控制处理函数
摘要: 何为服务?Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这种服务非常适合在服务器上使用,或任何时候,为了不影 ...
- python 编程服务_Python编写Windows Service服务程序
如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32,自己去下 ...
- C#写Windows Service(windows服务程序)
背景: 要学习使用一个新东西,我们必须知道他是个什么东西.对于我们此次研究的windows服务来说,他又是个什么东西,其实也没有什么高深的了. windows service概述: 一个 ...
- C# 编写Windows Service(windows服务程序)
Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就 ...
- Atitit.Java exe bat 作为windows系统服务程序运行
Atitit.Java exe bat 作为windows系统服务程序运行 1. 使用SC命令+srvany.exe (不错,推荐)+net start1 1.1. First 创建一个java的运 ...
- python写一个服务_写一个Python的windows服务
1. 安装pywin32和pyinstaller pip install pywin32 pip install pyinstaller 2.写一个服务Demo # -*- coding: utf-8 ...
- 用 VC++ 2008 编写 Windows Service(系统服务)
用 VC++ 2008 编写 Windows Service(系统服务) 2008-03-30 08:08 现在许多 Windows Service 应用都可以用 c# 很好的完成,不过毕竟是托管代码 ...
- Visual Studio.net 2010 Windows Service 开发,安装与调试
本示例完成一个每隔一分钟向C:\log.txt文件写入一条记录为例,讲述一个Windows Service 程序的开发,安装与调试 原程序,加文档示例下载 /Files/zycblog/Sou ...
最新文章
- 2021腾讯数字生态大会:腾讯安全聚焦安全共建,护航数字经济发展
- php多选框怎么传值,php多选框表单checkbox参数传递的问题解析
- matlab 1 f噪声,跪求1/f噪声生成代码解释!!
- ROS学习(二):在ubuntu 16.04安装ROS Kinetic
- BottomNavigationView处理只有2个tab的情况
- Android 多进程开发
- php window.onload,tp_window.onload+相应操作
- 新增10亿元作支持 重庆将多举措提升科技创新能力
- amd玄冰400怎么拆图解_【装机帮扶站】第735期:“无货”当道的京东年底大预售来了(AMD篇)...
- 【转】把我这段时间的教训分享一下,希望北邮校友们以我为鉴。
- 计算机硬件基本维护方法,计算机硬件全面维护的原则与方法探究
- python计算时差
- uboot dnw 源码分析
- wps不想让读个文档显示为多个标签页,而是打开多个窗口,方便多个文档的比对工作?
- 产品设计与计算机的关系,全面解读工业设计、产品设计和机械设计三者的关联和区别...
- java记事本教程_Java实战_手把手编写记事本
- Latex基本语法总结
- java中的比较方法
- 定位(定位模式和边偏移)
- Ibatis使用大于小于