用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的 ...
随机推荐
- Xcode not building app with changes incorporated
Did you clean the build folder by pressing command while the cursor is on the clean option? Are you ...
- [转]Linux统计代码行数
wc -l *.c *.h 就可以知道当前目录下的所有c 和 h 文件的行数的详细信息.很不错 如果要递归,可以配合其他命令一起使用 当前目录及子目录: find . -name *.c |xargs ...
- 设计模式C++实现——工厂方法模式
模式定义: 工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个. 工厂方法让类把实例化推迟到子类. 模式结构: Creator是一个类,它实现了全部操纵产品的方法,但不实现工厂方法 ...
- register_shutdown_function函数详解
设定错误和异常处理三函数 register_shutdown_function(array(‘Debug’,'fatalError’)); //定义PHP程序执行完成后执行的函数 set_error_ ...
- android binder 机制三(匿名Service)
什么是匿名Service?凡是没有到ServiceManager上注冊的Service,都是匿名Service. 还是拿上一篇的样例来举例,看代码: status_t MediaPlayer::set ...
- 系统去掉 Android 4.4.2 的StatusBar和NavigationBar
1. System Bar简单介绍 在Android4.0 (API Level 14)及更高版本号中.System Bar由Status Bar<位于顶部>和Navigation Bar ...
- Java中常见的注解
Java中常见的注解 1.JDK自带的注解@Override @Deprecated @Suppvisewarnings 常见第三方注解 Spring:@Autowired @Service ...
- 我对hibernate和mybatis框架的比較
系统在选择操作数据库的框架上面,究竟是选择hibernate,还是mybatis. 首先说下两者的原理,假设你要关联几张表做查询,查出20条记录: 1.假设是mybatis SELECT * FR ...
- Android GUI系统学习1:Gralloc
Gralloc模块是从Android Eclair(android 2.1)開始增加的一个HAL模块,Gralloc的含义为是Graphics Alloc(图形分配).他对上为libui提供服务,为其 ...
- Pattern: API Gateway / Backend for Front-End
http://microservices.io/patterns/apigateway.html Pattern: API Gateway / Backend for Front-End Contex ...