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 ...
随机推荐
- 配置yum,nc,telnet
一.学习中问题 最近学习在学习Hadoop的一个子项目Zookeeper,在测试其中的“四字命令”---”echo ruok|nc localhost 2181“时发现命令无法被识别,如下图所示: [ ...
- ajax传参data里面的键是一个变量的解决方法
直接用这种方式来传参,比如bean中有字段 username password,则是 data[username] = "用户名"; data[password] = " ...
- [LOJ#6068]. 「2017 山东一轮集训 Day4」棋盘[费用流]
题意 题目链接 分析 考虑每个棋子对对应的横向纵向的极大区间的影响:记之前这个区间中的点数为 \(x\) ,那么此次多配对的数量即 \(x\) . 考虑费用流,\(S\rightarrow 横向区间 ...
- 架构师修炼 II - 表达思维与驾驭方法论
开篇之前我想先说说当年开发的那点事儿:大约10年前吧,我还是一个程序员的时候经常都是遇到这样的项目开发流程: 解决方案 :满足客户目的和投标用的一堆文档(不少还是互联网上抄的) ,是以Word为主的纯 ...
- 浅谈android Service和BroadCastReceiver
1.题记 Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序. 广播接收者(BroadcastRece ...
- Qt连接数据库的两种方法
我曾经想过,无论在哪个平台下开发,都不要再接触SQL Server了,但显然不行.我们是来看世界的,不是来改变世界的,想通就好. 前两天,尝试了一下Qt下远程访问数据库.在macOS下,用Qt 5.1 ...
- Unity5.6之前版本VRTK插件基础交互
一.VR运行环境配置: 安装steam,在steam上安装SteamVR驱动. 在Unity项目中需要导入VRTool插件包(已上传服务器),里面包含两个插件一个是SteamVR插件,一个是VRTK插 ...
- 微软职位内部推荐-Senior Software Development Engineer_Commerce
微软近期Open的职位: Are you looking for a high impact project that involves processing of billions of dolla ...
- linux 常用命令-用户、用户组管理
注:本篇只涉及常用命令,全部命令可以通过help帮助查看. (1)type useradd #查看命令属于内部命令还是外部命令,内部命令是嵌在linux的shell中,外部命令存储在路径中 (2) ...
- ORACLE创建数据库时无法创建目录
ORACLE创建数据库时无法创建目录,如图所示信息 原因:没有创建写入的权限 解决:修改文件夹权限即可 F:\oracle\product\10.2.0\db_1\cfgtoollogs\dbca 增 ...