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 ...
随机推荐
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
- map put
public class test { static Map<String, Map<String, Integer>> mapB = new HashMap<Strin ...
- 清北学堂模拟赛d3t3 c
分析:一开始拿到这道题真的是无从下手,暴力都很难打出来.但是基本的方向还是要有的,题目问的是方案数,dp不行就考虑数学方法.接下来比较难想.其实对于每一行或者每一列,我们任意打乱顺序其实对答案是没有影 ...
- MySQL5.7出现无法启动服务等问题
MySQL5.7版本后有点不同,就是没有DATA文件夹.总是莫名其妙出现一些错误.比如连不上数据库了,出现错误 can't connect mysql on localhost,键入 net star ...
- 使用Java VisualVM配置Java应用程序/分析CPU或内存的使用情况(转)
以下内容翻译自(机翻):https://baptiste-wicht.com/posts/2010/07/profile-applications-java-visualvm.html 当您需要发现应 ...
- RDS for MySQL Online DDL 使用
https://help.aliyun.com/knowledge_detail/41733.html?spm=5176.7841698.2.17.u67H3h
- HTML5:去除IE10中输入框和密码框的X按钮和小眼睛
在IE10和之后的IE版本中,当在输入框和密码框中输入的时候,后面会自动出现X按钮和小眼睛,如下图所示: 令人苦恼的是,这个效果只有IE才有,其它浏览器是没有这个功能的.为了统一,我们就需要去掉这个 ...
- ORACLE11G设置IP訪问限制
出于数据安全考虑,对oracle数据库的IP做一些限制,仅仅有固定的IP才干訪问. 改动$JAVA_HOME/NETWORK/ADMIN/sqlnet.ora文件 添加下面内容(红色表示凝视): #开 ...
- ios调用dismissViewController的一个小陷阱
我们的APP从启动到进入主页面.是通过presentViewController构造了一个ViewController序列,类似于首页 -> 登陆页 -> 启动载入页 -> 主页面 ...
- 【面试】【Spring常见问题总结】【07】
[常见面试问题总结文件夹>>>] 61.Spring IoC容器的依赖有两层含义: Bean依赖容器:也就是说Bean要依赖于容器,这里的依赖是指容器负责创建Bean并管理Bean的 ...