用python写windows服务
用python写windows服务(1)
以python2.5 为例
需要软件
* python 2.5
* pywin32(与2.5 版本相匹配的)
Service Control Manager (SCM)
服务管理器(SCM) 是windows NT的 一部分,所有服务必须通过SCM 注册,SCM负责启动,停止服务等。
当一个进程通过SCM注册后, 有如下特质:
* 运行该进程的用户,未必是当前登录的用户。
* 该进程如果依赖其他服务,哪么该服务启动前,依赖服务回启动。该服务停止后,依赖服务会停止。(估计是应用计数减1)
* 服务可知计算机启动后自动启动,或者手动启动。
windows NT 通过执行一个进程开始相应服务。一旦这个进程执行,它需要告知SCM它实际上是作为一个服务运行。还需要传给SCM一个控制句柄(control handler)。其实就是一个函数,用于处理SCM 发来的相关信息。 当服务被停止时, SCM传信息给控制句柄。服务本身负责处理该请求,并停止本身服务。
pywin32 服务相关module
* win32service 实现了Win32服务功能。
* win32serviceutil 对api的包装,始面向用户的接口更友好。
* PythonService.exe 使用pywin32 服务器,它必须先注册。
下面重点讲 win32serviceutil
服务框架类
win32serviceutil.ServiceFramework
__init__
构造函数,注册ServiceCtrlHandler给SCM
ServiceCtrlHandler
本服务的control handler 的默认实现。该函数会查询类内的函数名,用以判断该服务提供哪些控制接口,比如类内有SvcPause 函数。则会认为该服务可以被暂停。
SvcRun
服务入口点。服务运行,就是运行这个函数。
简单示例
代码:
# SmallestService.py
#
# A sample demonstrating the smallest possible service written in Python. import win32serviceutil
import win32service
import win32event class SmallestPythonService(win32serviceutil.ServiceFramework):
_svc_name_ = "SmallestPythonService"
_svc_display_name_ = "The smallest possible Python 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) def SvcStop(self):
# Before we do anything, tell the SCM we are starting the stop process.
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# And set my event.
win32event.SetEvent(self.hWaitStop) def SvcDoRun(self):
# We do nothing other than wait to be stopped!
win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) if __name__=='__main__':
win32serviceutil.HandleCommandLine(SmallestPythonService)
安装服务
进入PythonService.exe所在目录, 命令行下执行命令
(PATH)>PythonService.exe /register
Registering the Python Service Manager...
安装服务
C:\Scripts> SmallestService.py install
Installing service SmallestPythonService to Python class
C:\Scripts\SmallestService.SmallestPythonService
Service installed
C:\Scripts>
启动服务
C:\Scripts> python.exe SmallestService.py start
Starting service SmallestPythonService
C:\Scripts>
启动确认
C:\Scripts> python.exe SmallestService.py start
Starting service SmallestPythonService
Error starting service: An instance of the service is already running.
C:\Scripts>
停止服务
C:\Scripts> python.exe SmallestService.py stop
Stopping service SmallestPythonService
C:\Scripts>
用python写windows服务的更多相关文章
- 【python】使用python写windows服务
背景 运维windows服务器的同学都知道,windows服务器进行批量管理的时候非常麻烦,没有比较顺手的工具,虽然saltstack和ansible都能够支持windows操作,但是使用起来总感觉不 ...
- 不用写Windows服务实现定时器功能(FluentScheduler )
MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲击与影响,那这个世界永远像现在不变就行了,大家都好好的,待在自 ...
- Python做windows服务
Python做windows服务(多进程服务),并结束多进程 Python中_,__,__xx__的区别 在注册MyWinService服务时,再使用 "sc delete 服务器名称&qu ...
- 写一个Python的windows服务
1. 安装pywin32和pyinstaller pip install pywin32 pip install pyinstaller 2.写一个服务Demo # -*- coding: utf-8 ...
- 使用Python写Windows Service服务程序
1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...
- Python-windows服务-重启自动化
一. 前言 有了上一篇的“python初学”的基础,咱们就有了python的开发包,有了开发环境IDE,那我们就可以干活了.我的第一个选题就是让我们的windows服务可以按照我们的意愿进行自动重启. ...
- python管理Windows服务
上一篇介绍了pywin32模块,它的win32service子模块提供了对服务管理API的包装,相关API如下: ChangeServiceConfig ChangeServiceConfig2 Cl ...
- c#写windows服务
序言 前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而IIs又不稳定因素,如果重启IIs就要打开页面才能运行项目.有不便之处,就改用Windows服务实现.这篇就总 ...
- Python 写Windows Service服务程序
1.需求 为什么要开发一个windows服务呢?之前做一个程序,必须要读取指定目录文件License, 因为其他程序也在读取这指定目录的License文件,且License不同时会修改License的 ...
随机推荐
- MMT事务处理来源类型-INV_OBJECT_GENEALOGY.GETSOURCE
INV_OBJECT_GENEALOGY.GETSOURCE (MTL_MATERIAL_TRANSACTIONS.ORGANIZATION_ID, ...
- 高级算法设计讲义 Lecture Notes for Advanced Algorithm Design
(Last modification: 2012-12-17) Textbooks: (1) David Williamson, David Shmoys. The Design of Approxi ...
- docker 搭建linux samba
https://hub.docker.com/r/jenserat/samba-publicshare/ 需要共享目录 只需直接 挂载容器映射即可
- Qt:解析命令行
Qt从5.2版開始提供了两个类QCommandLineOption和QCommandLineParser来解析应用的命令行參数. 一.命令行写法 命令行:"-abc" 在QComm ...
- 第04章-VTK基础(2)
[译者:这个系列教程是以Kitware公司出版的<VTK User's Guide -11th edition>一书作的中文翻译(出版时间2010年,ISBN: 978-1-930934- ...
- Ubuntu 16.04 LTS 配置 Jupyter notebook 为服务器
原材料: Ubuntu 16.04 LTS 64bit 已经配置好 IPython 和 Jupyter (安装步骤可以参照:http://www.cnblogs.com/McKean/p/619497 ...
- TCP连接建立与终止,及状态转换
TCP连接建立 三路握手 三路握手发生在客户端发起connect请求到服务端accept返回中,在三路握手发生前,服务端必须准备好接受外来连接,这通常通过服务端调用 (socket.bind.list ...
- eclipse配置android
先在eclipse中安装ADT插件,install内点击add,name:ADT, URL:http://dl-ssl.google.com/android/eclipse/ 之后直接finish就好 ...
- Golang-interface(二 接口与nil)
github: https://github.com/ZhangzheBJUT/blog/blob/master/nil.md 一 接口与nil 前面解说了go语言中接口的基本用法,以下将说一说nil ...
- HDU 4821 String 字符串hash
String Problem Description Given a string S and two integers L and M, we consider a substring of S ...