Python3编写Windows服务程序
最近做了公司签到的小工具,有同事要求做成Windows服务,开机自启。先说下怎么用Python写Windows服务程序。
#encoding=utf-8
import win32serviceutil
import win32service
import win32event
import win32timezone
import os class PythonService(win32serviceutil.ServiceFramework):
_svc_name_ = 'PythonService' #服务名称
_svc_display_name_ = 'regMeal'
_svc_description_ = '每天晚上6:40后自动签到' def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
self.logger = self._getLogger()
self.run = True def _getLogger(self):
import inspect
import logging
logger = logging.getLogger('[PythonService]')
this_file = inspect.getfile(inspect.currentframe())
dirpath = os.path.abspath(os.path.dirname(this_file))
handler = logging.FileHandler(os.path.join(dirpath,'service.log'))
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
return logger def SvcDoRun(self):
import time
import readconfig
import regMeal
self.logger.info('service is run...')
while self.run:
self.logger.info('service is running...')
paraList = readconfig.readConfig()
bFlag = regMeal.main(paraList[0],paraList[1],paraList[2])
time.sleep(2) def SvcStop(self):
self.logger.info('service is stop.')
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.run = False if __name__ == '__main__':
import sys
import servicemanager
if len(sys.argv) == 1:
try:
evtsrc_dll = os.path.abspath(servicemanager.__file__)
servicemanager.PrepareToHostSingle(PythonService)
servicemanager.Initialize('PythonService',evtsrc_dll)
servicemanager.StartServiceCtrlDispatcher()
except win32service.error as details:
import winerror
if details == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
win32serviceutil.usage()
else:
win32serviceutil.HandleCommandLine(PythonService)
推荐大家对比网上其他的人的代码对照看下。SvcDoRun这个函数里面放的就是你实际想做的事情。
安装服务
python PythonService.py install
让服务自动启动
python PythonService.py --startup auto install
启动服务
python PythonService.py start
重启服务
python PythonService.py restart
停止服务
python PythonService.py stop
删除/卸载服务
python PythonService.py remove
我自己是用pyinstaller打包成exe给别人用的。
PS:脚本编好之后,调试了很久。刚开始会报服务无法启动的问题。具体的排查方式可以看Windows事件查看。
Python3编写Windows服务程序的更多相关文章
- C语言编写Windows服务程序
原文:C语言编写Windows服务程序 #include <Windows.h> #include <stdio.h> #define SLEEP_TIME 5000 // 间 ...
- 编写windows服务程序
2012-11-02 08:54 (分类:计算机程序) windows服务是一个运行在后台并实现勿需用户交互的任务的控制台程序,对于隐藏程序有很大帮助. 用了几天时间概括了编写windows服务程序的 ...
- 用 C 语言编写 Windows 服务程序的五个步骤
Windows 服务被设计用于需要在后台运行的应用程序以及实现没有用户交互的任务.为了学习这种控制台应用程序的基础知识,C(不是C++)是最佳选择.本文将建立并实现一个简单的服务程序,其功能是查询系统 ...
- 用C语言编写Windows服务程序的五个步骤
Windows 服务被设计用于需要在后台运行的应用程序以及实现没有用户交互的任务.为了学习这种控制台应用程序的基础知识,C(不是C++)是最佳选择.本文将建立并实现一个简单的服务程序,其功能是查询系统 ...
- C#编写Windows服务程序图文教程
安装服务程序C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe 要安装的服务程序路径(如F:\***.exe)卸载服务程序C: ...
- C#编写Windows服务程序 (服务端),client使用 消息队列 实现淘宝 订单全链路效果
需求: 针对 淘宝提出的 订单全链路 产品接入 .http://open.taobao.com/doc/detail.htm?id=102423&qq-pf-to=pcqq.group oms ...
- C#编写windows服务程序
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- C#编写Windows服务程序图文教程(转载)
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- 用Visual C#创建Windows服务程序
一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...
随机推荐
- 第六节:web爬虫之urllib(二)
二.urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, metho ...
- 一个神奇的PHP框架:Phalcon 之初识
前言 公司的APP响应速度比较慢,公司大神决定使用C语言编写的PHP框架Phalcon 代替原来的框架,响应速度有比较大的提升.以前只是听说过,没有深入的了解过.即然工作中有用到,便花点时间了解了下, ...
- Intellij Idea 13:导入openfire源代码
网络上已经有篇关于openfire导入到Intellij Idea的文章(http://www.th7.cn/Program/java/201404/187018.shtml),不过在我导入的过程中, ...
- gradle配置国内的镜像
gradle配置国内的镜像 学习了:http://blog.csdn.net/stdupanda/article/details/72724181 http://blog.csdn.net/lj402 ...
- ajax——dom基础
javascript中dom实现能够使我们在ajax中通过javascript代码对html和xml数据进行dom方式操作,从而做到页面的动态改动更新和数据的提取处理. dom概念 dom文档对象模型 ...
- HBase编程实例
摘要:在前文中安装了Hbase,通过Hbase shell能够进行一些操作.可是和实际的编程实例联系起来不方便,因此本文介绍有关Hbase编程的实例. 一.使用Eclipse开发HBase应用程序 1 ...
- mysql 忘记了root的password(linux下解决方法,window同理)
mysql 忘记了root的password的时候的解决步骤, 1: cd /etc/mysql/(进入mysql的配置文件夹) 2:vim my.cnf \skip-grant-tables(进入m ...
- iOS学习(3)
4. 这个写法会出什么问题: @property (copy) NSMutableArray *array; 两个问题:1.加入,删除,改动数组内的元素的时候,程序会由于找不到相应的方法而崩溃.由于 ...
- C# SuperWebSocket服务端、客户端学习(三)
1.打开VS2012,新建一个windows窗体程序,选择.NET4.0版本 2.添加引用 SuperSocket的dll文件( SuperSocket.Common.dll, SuperSocket ...
- 生活的 tricks
1. 远距离传递 传真(需要附近有传真机):发 QQ.微信拍照,自己打印: 2. 超市的设计 如果是两层的话,入口一定在第一楼,出口在第二楼,也即当你需要出去的时候,需要贯穿整个超市: 用的在第一楼: ...