python to be Windows Daemon
参考:http://assback.iteye.com/blog/1731565
安装
pywin32-.win32-py2..exe #32bit
pywin32-.win-amd64-py2..exe #64bit
http://blog.csdn.net/ghostfromheaven/article/details/8604738
import win32serviceutil
import win32service
import win32event class PythonService(win32serviceutil.ServiceFramework):
"""
Usage: 'PythonService.py [options] install|update|remove|start [...]|stop|restart [...]|debug [...]'
Options for 'install' and 'update' commands only:
--username domain\username : The Username the service is to run under
--password password : The password for the username
--startup [manual|auto|disabled|delayed] : How the service starts, default = manual
--interactive : Allow the service to interact with the desktop.
--perfmonini file: .ini file to use for registering performance monitor data
--perfmondll file: .dll file to use when querying the service for
performance data, default = perfmondata.dll
Options for 'start' and 'stop' commands only:
--wait seconds: Wait for the service to actually start or stop.
If you specify --wait with the 'stop' option, the service
and all dependent services will be stopped, each waiting
the specified period.
"""
#服务名
_svc_name_ = "PythonService"
#服务显示名称
_svc_display_name_ = "Python Service Demo"
#服务描述
_svc_description_ = "Python service demo." def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.logger = self._getLogger()
self.isAlive = True def _getLogger(self):
import logging
import os
import inspect 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
self.logger.error("svc do run....")
while self.isAlive:
self.logger.error("I am alive.")
time.sleep(1)
# 等待服务被停止
win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) def SvcStop(self):
# 先告诉SCM停止这个过程
self.logger.error("svc do stop....")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# 设置事件
win32event.SetEvent(self.hWaitStop)
self.isAlive = False if __name__=='__main__':
win32serviceutil.HandleCommandLine(PythonService)
python to be Windows Daemon的更多相关文章
- Python环境搭建(windows)
Python环境搭建(windows) Python简介 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/),是一种面向对象.直译式计算机编程语言,具有近二十年的发展历史,成 ...
- Fix Python 3 on Windows error Microsoft Visual C++ 14.0 is required
Fix Python 3 on Windows error Microsoft Visual C++ 14.0 is required Fix the error for Python 3.6 and ...
- [Python]Threading.Thread之Daemon线程
之前对Daemon线程理解有偏差,特记录说明: 一.什么是Daemon A thread can be flagged as a "daemon thread". The sign ...
- python MySQLdb在windows环境下的快速安装
python MySQLdb在windows环境下的快速安装.问题解决方式 使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 Python MySQLdb在Linux下 ...
- Python通过调用windows命令行处理sam文件
Python通过调用windows命令行处理sam文件 以samtools软件为例 一.下载或者索取得到windows版本的samtools软件,解压后如下: 进入文件内部,有如下几个文件: 二.将s ...
- python安装在windows server2008,故障排除
python安装在windows server2008,故障排除 我也在虚拟机上的win2008安装python2.7.9多次回滚了.搜了一通 Windows Server 2003/2008无法 ...
- Python extensions for Windows
Python extensions for Windows pywin32 214 Python extensions for Windows Maintainer: Mark Hammond Hom ...
- python如何通过windows命令行运行一个python程序文件?
python如何通过windows命令行运行一个python程序文件? cmd 进入到py文件对应目录下或者直接在上面的文件地址栏输入cmd,敲入回车 定位到对应的目录下 输入python xxx.p ...
- python+msf 制作 windows远控
最近分析的一个远控,后发现是meterpreter rever http shell,文件是个打包的python(打包成exe),感谢wstone的指导~ 创建dll ./msfpayload win ...
随机推荐
- Spring--Spring容器
在使用Spring所提供的各种丰富而神奇的功能之前,必须要在Spring IoC容器中装配好Bean,并建立Bean和Bean之间的关联关系. Spring提供了多种配置方式来实现Bean的装配.但在 ...
- Hibernate SQL优化技巧dynamic-insert="true" dynamic-update="true"
最近正在拜读Hibernate之父大作<Java Persistence with Hibernate>,颇有收获.在我们熟悉的Hibernate映射文件中也大有乾坤,很多值得我注意的地方 ...
- #ifdef的用法【转】
#ifdef的用法 #ifdef的用法灵活使用#ifdef指示符,我们可以区隔一些与特定头文件.程序库和其他文件版本有关的代码.代码举例:新建define.cpp文件 #include &qu ...
- js中url解码
var name = window.location.search.substr(4,30);//name = decodeURI(name);//使用示例document.getElementByI ...
- [设计模式] javascript 之 模板方法模式
模板方法模式说明 定义:定义方法操作的骨架,把一些具体实现延伸到子类中去,使用得具体实现不会影响到骨架的行为步骤! 说明:模式方法模式是一个继承跟复用的典型模式,该模式定义了一个抽象类,Abstrac ...
- ngCordova插件安装使用
为什么ngCordova ngCordova是在Cordova Api基础上封装的一系列开源的AngularJs服务和扩展,让开发者可以方便的在HybridApp开发中调用设备能力,即可以在Angul ...
- 【Solr】 solr对拼音搜索和拼音首字母搜索的支持
问:对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输入拼音和拼音首字母就给出商品的信息,怎么办呢? 实现方式有2种,但是他们其实是对应的. 用lucene实现 1.建索引, 多建一个索引字段 ...
- git 初始化
Git global setup git config --global user.name "杨清1" git config --global user.email " ...
- Maven 跳过测试目录
在命令行使用 mvn install -Dmaven.skipTests 或 mvn install -Dmaven.test.skip=true 或在pom.xml设置 <build> ...
- join 和 union 区别
JOIN和UNION区别 join 是两张表做交连后里面条件相同的部分记录产生一个记录集,union是产生的两个记录集(字段要一样的)并在一起,成为一个新的记录集 .JOIN用于按照ON条件联接两个表 ...