用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的 ...
随机推荐
- MFC中的几种播放声音的方法
一.播放声音文件的简单方法 在VC++ 中的多媒体动态连接库中提供了一组与音频设备有关的函数.利用这些函数可以方便地播放声音.最简单的播放声音方法就是直接调用VC++中提供的声音播放函 数BOOL s ...
- Java 5/Java 6/Java7/Java 8新特性收集
前言: Java 8对应的JDK版本为JDK8,而官网下载回来安装的时候,文件夹上写的是JDK1.8,同一个意思.(而这个版本命名也是有规律的,以此类推) 一.Java 5 1.https://seg ...
- ORACLE 内部原理
http://www.ohsdba.cn/index.php?m=Article&a=index&id=46 内部原理 2016-05-04• 如何使用BBED 2016-04-16• ...
- Camtasia Studio录制屏幕字迹不清晰的原因
Camtasia Studio这是一个很优秀的屏幕录像软件,功能强大且录制效果出色,支持众多格式输出: 之前一直用它录制视频的都很正常,但这次换系统后再重新安装后录制视频时,发现输出的视频画质不佳,文 ...
- C# UserControl 判断是否是设计模式中
In Windows Forms application, we can use Control.IsInDesignMode or LicenseManager.UsageMode == Licen ...
- Python机器学习--手写体识别(KNN+MLP)
MLP实现 调整参数比较性能结果 # -*- coding: utf-8 -*- """ Created on Wed Aug 30 21:14:38 2017 @aut ...
- PostgreSQL触发器的使用
原文: https://www.yiibai.com/postgresql/postgresql-trigger.html -------------------------------------- ...
- win10中显示wpcap.dll丢失的处理方式
win10中显示wpcap.dll丢失的处理方式 学习了:https://jingyan.baidu.com/article/4f34706e30e673e387b56dd8.html 直接安装Win ...
- 通过Java反射做实体查询
我们在使用hibernate的时候,查询的时候都会和实体中的一些字段相结合去查询,当然字段少了,还算是比较简单,当字段多了,就不那么容易了,所以就自己写了个方法,根据实体中的字段信息去查询,废话不多说 ...
- Allegro改动shape网络节点
使用Allegro时改动shape的网络节点方法: ①选择shape->Select Shape or Void/Cavity ②选择要改动的shape ③点击(...)改动网络节点的名字 ④改 ...