Python制作Windows系统服务
来源:互联网 发布:驾驶模拟软件 编辑:程序博客网 时间:2024/06/16 20:49
Python制作Windows系统服务
最近有个Python程序需要安装并作为Windows系统服务来运行,过程中碰到一些坑,整理了一下。
Python服务类
首先Python程序需要调用一些Windows系统API才能作为系统服务,具体内容如下:
#!/usr/bin/env python# -*- coding: utf-8 -*-import sysimport timeimport win32apiimport win32eventimport win32serviceimport win32serviceutilimport servicemanagerclass MyService(win32serviceutil.ServiceFramework): _svc_name_ = "MyService" _svc_display_name_ = "My Service" _svc_description_ = "My Service" def __init__(self, args): self.log('init') win32serviceutil.ServiceFramework.__init__(self, args) self.stop_event = win32event.CreateEvent(None, 0, 0, None) def SvcDoRun(self): self.ReportServiceStatus(win32service.SERVICE_START_PENDING) try: self.ReportServiceStatus(win32service.SERVICE_RUNNING) self.log('start') self.start() self.log('wait') win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE) self.log('done') except BaseException as e: self.log('Exception : %s' % e) self.SvcStop() def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) self.log('stopping') self.stop() self.log('stopped') win32event.SetEvent(self.stop_event) self.ReportServiceStatus(win32service.SERVICE_STOPPED) def start(self): time.sleep(10000) def stop(self): pass def log(self, msg): servicemanager.LogInfoMsg(str(msg)) def sleep(self, minute): win32api.Sleep((minute*1000), True)if __name__ == "__main__": if len(sys.argv) == 1: servicemanager.Initialize() servicemanager.PrepareToHostSingle(MyService) servicemanager.StartServiceCtrlDispatcher() else: win32serviceutil.HandleCommandLine(MyService)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
pyinstaller打包
pyinstaller -F MyService.py
- 1
- 1
测试
# 安装服务dist\MyService.exe install# 启动服务sc start MyService# 停止服务sc stop MyService# 删除服务sc delete MyService
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
转载请以链接形式标明本文链接
本文链接:http://blog.csdn.net/kongxx/article/details/65435076
阅读全文
0 0
- Python制作Windows系统服务
- Python制作Windows系统服务
- windows下python监控系统服务
- 制作windows服务程序
- 制作windows服务程序
- 制作windows服务安装服务
- VC制作系统服务
- windows环境将Python脚本做成系统服务
- 制作Windows服务安装包
- Windows服务安装包制作
- C#制作Windows service服务
- Windows运行Python服务
- WINDOWS系统服务详解
- Windows系统服务大全
- windows系统服务优化
- WINDOWS系统服务
- windows系统服务设置
- 卸载windows系统服务
- 阿里巴巴Android面试题
- 各种IT资讯公众号
- 激光雷达学习笔记(二)数据读取和显示
- JS 姓名、身份证号检测
- Monkey and Banana
- Python制作Windows系统服务
- POJ 1475 Pushing Boxes 笔记
- git 常用命令
- Servlet生命周期与工作原理
- 针对Apache poi的简单学习, 读写Excel
- Hibernate入门程序
- Java 8新特性终极指南
- 屏幕旋转适配问题
- Java : historical Java releases. Oracle Java Archive