Python之Windows服务
1、首先要安装pywin32-220.win-amd64-py2.7.exe
2、
SvcDoRun:服务启动的时候会执行的方法
SvcStop:服务停止的时候会执行的方法
# coding=utf-8
import sys
import logging
from logging.handlers import RotatingFileHandler
import datetime
import os
import win32serviceutil
import win32service
import win32event
import time reload(sys)
sys.setdefaultencoding('utf-8') def log():
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
# 必须要指定 sys.path[0] 得到的是这个文件所在的路径 如果不指定就跑到win32所在的路径下了
logPath = os.path.join(sys.path[0], 'log') if not os.path.exists(logPath):
os.makedirs(logPath)
handler = RotatingFileHandler(
os.path.join(logPath, '%s.txt' % datetime.date.today()), maxBytes=5 * 1024 * 1024, backupCount=10)
handler.setLevel(level=logging.INFO)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter)
logger.addHandler(handler) logger.error('error') class PythonService(win32serviceutil.ServiceFramework): # 服务名
_svc_name_ = "WinServiceTest"
# 服务显示名称
_svc_display_name_ = "WinServiceTest"
# 服务描述
_svc_description_ = "WinServiceTest description" def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) def SvcDoRun(self):
while True: # 要加while循环 否则服务只能运行1次 启动服务时有提示
log()
#死循环 服务停止不了
if win32event.WaitForSingleObject(self.hWaitStop, 5000) == win32event.WAIT_OBJECT_0:
break
def SvcStop(self):
# 先告诉SCM停止这个过程
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# 设置事件
win32event.SetEvent(self.hWaitStop)
# 必须要加 否则会出现Python could not import the service's module 错误代码1
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(PythonService)
注意点:
1)if __name__ == '__main__'必须要加,否则会报错。


2)while True也要加,如果只想运行一次可以不加。会有提示,说只运行了1次。

Python之Windows服务的更多相关文章
- Python做windows服务
Python做windows服务(多进程服务),并结束多进程 Python中_,__,__xx__的区别 在注册MyWinService服务时,再使用 "sc delete 服务器名称&qu ...
- 用python写windows服务
用python写windows服务(1) 以python2.5 为例需要软件 * python 2.5 * pywin32(与2.5 版本相匹配的) Service Control Ma ...
- Python-windows服务-重启自动化
一. 前言 有了上一篇的“python初学”的基础,咱们就有了python的开发包,有了开发环境IDE,那我们就可以干活了.我的第一个选题就是让我们的windows服务可以按照我们的意愿进行自动重启. ...
- 【python】使用python写windows服务
背景 运维windows服务器的同学都知道,windows服务器进行批量管理的时候非常麻烦,没有比较顺手的工具,虽然saltstack和ansible都能够支持windows操作,但是使用起来总感觉不 ...
- python管理Windows服务
上一篇介绍了pywin32模块,它的win32service子模块提供了对服务管理API的包装,相关API如下: ChangeServiceConfig ChangeServiceConfig2 Cl ...
- 写一个Python的windows服务
1. 安装pywin32和pyinstaller pip install pywin32 pip install pyinstaller 2.写一个服务Demo # -*- coding: utf-8 ...
- python实现编写windows服务
使用python编写windows服务 最近测试服务器上经常发生磁盘空间不足,每次手动清除比较麻烦,所以写个windows服务定时清理下.中间也遇到过几个坑,一起记录下来. 1.python实现win ...
- Python搭建调用本地dll的Windows服务(浏览器可以访问,附测试dll64位和32位文件)
一.前言说明 博客声明:此文链接地址https://www.cnblogs.com/Vrapile/p/14113683.html,请尊重原创,未经允许禁止转载!!! 1. 功能简述 (1)本文提供生 ...
- 一次使用Python连接数据库生成二维码并安装为windows服务的工作任务
最近有一个需求,在现有生产系统上的人员库中增加一个此人员关键信息的二维码,支持文字版和跳转版两种方式,与报表工具关联,可打印.以windows服务方式,定时检查,只要发现某人员没有此二维码信息,就生成 ...
随机推荐
- 巧用Beyond Compare帮你更好校对文稿
我们平常所说的校对工作,大多数指的是书本.文章出版前的对其进行的原稿比对工作.该工作要求极为细致,校对者需对文稿中的标点.编号.序号等细微部分进行认真比对,以保证出版物的质量.其实我们在日常的学习工作 ...
- yii2.0 设置默认路由
在config/web.php 添加 $config = [ 'defaultRoute' => 'login/login', ];
- 解决Maven项目中pom.xml文件报错(Failure to transfer....)的问题
打开pom.xml文件,查看错误,如果错误类型为:Failure to transfer.........之类的,则表明你的pom中依赖的jar包没有完全下载. 解决方法:找到你本地的maven仓库, ...
- 配置Nginx 扩展实现图片剪裁
在此之前需要安装ngx_http_image_filter_module,如果是采用的Docker的话可以看看我历史文章. 然后修改配置文件,增加几个location模块,配置如下,仅供参考 serv ...
- 【mq读书笔记】消息拉取长轮训机制(Broker端)
RocketMQ并没有真正实现推模式,而是消费者主动想消息服务器拉取消息,推模式是循环向消息服务端发送消息拉取请求. 如果消息消费者向RocketMQ发送消息拉取时,消息未到达消费队列: 如果不启用长 ...
- 区块链V1版本实现之五
代码重构: block.go文件: package main import ( "crypto/sha256" ) //定义区块结构 type Block struct { //前 ...
- 6.2 Binding基础
WPF最为核心的思想是数据驱动UI,实现这一技术的基石就是绑定技术(binding).如果把Binding比作数据的桥梁,那么它的两端分别是源(Source)和目标(Target),Binging是架 ...
- Fist—— 团队展示
作业要求 软件工程1班 团队名称 Fist 这个作业的目标 团队合作开发项目,加强团队合作,进一步了解相应岗位. 作业正文 https://www.cnblogs.com/team4/p/137730 ...
- 在 Spring Boot 配置 Kafka 安全认证
spring: kafka: bootstrap-servers: IP:端口 listener: missing-topics-fatal: false properties: sasl: mech ...
- 浅尝 Elastic Stack (一) Elasticsearch、Kibana、Beats 安装
Elastic Stack 包括 Elasticsearch.Kibana.Beats 和 Logstash,也称为 ELK Stack.能够安全可靠地获取任何来源.任何格式的数据,然后实时地对数据进 ...