python管理Windows服务
上一篇介绍了pywin32模块,它的win32service子模块提供了对服务管理API的包装,相关API如下:
ChangeServiceConfig
ChangeServiceConfig2
CloseServiceHandle
ControlService
CreateDesktop
CreateService
CreateWindowStation
DeleteService
EnumDependentServices
EnumServicesStatus
EnumServicesStatusEx
EnumWindowStations
GetProcessWindowStation
GetServiceDisplayName
GetServiceKeyName
GetThreadDesktop
GetUserObjectInformation
LockServiceDatabase
OpenDesktop
OpenInputDesktop
OpenSCManager
OpenService
OpenWindowStation
QueryServiceConfig
QueryServiceConfig2
QueryServiceLockStatus
QueryServiceObjectSecurity
QueryServiceStatus
QueryServiceStatusEx
SetServiceObjectSecurity
SetServiceStatus
SetUserObjectInformation
StartService
UnlockServiceDatabase
有了这些API,完成一些服务的基本操作,比如安装服务、停止服务、启动服务等完全不在话下,只不过都是直接调用API而已。
不过pywin32还有一个对win32service模块的包装,叫做 win32serviceutil,使用它可以更方便地对服务进行控制。
InstallService(pythonClassString, serviceName, displayName, startType = None,
errorControl = None, bRunInteractive = 0, serviceDeps = None, userName = None,
password = None, exeName = None, perfMonIni = None,perfMonDll = None,
exeArgs = None,description = None, delayedstart = None) #安装服务 ChangeServiceConfig(pythonClassString, serviceName, startType = None,
errorControl = None, bRunInteractive = 0, serviceDeps = None,
userName = None, password = None,exeName = None, displayName = None,
perfMonIni = None, perfMonDll = None,exeArgs = None,
description = None, delayedstart = None) #更改服务设置 #以上两个函数的第一个参数pythonClassString是为Python编写的Windows服务准备的,非Python服务则不需要,直接填None即可 RemoveService(serviceName) #删除服务
ControlService(serviceName, code, machine = None) #控制服务,code的具体定义参考MSDN的对ControlService的介绍
StopService(serviceName, machine = None) #停止服务
StartService(serviceName, args = None, machine = None) #启动服务
RestartService(serviceName, args = None, waitSeconds = 30, machine = None) #重启服务
QueryServiceStatus(serviceName, machine=None) #查询服务状态
大部分API只需要提供服务名称就可以了,调用起来非常简单。
比如 win32serviceutil.StopService("Spooler") 就可以停止Spooler服务了。
对于 win32serviceutil.QueryServiceStatus()这个函数,它实际返回的是win32service.QueryServiceStatus的返回结果。
从MSDN和pywin32的帮助文件可知,win32service.QueryServiceStatus的返回值是一个元组,如下:
SERVICE_STATUS Object
A Win32 service status object is represented by a tuple:
Items
[0] int : serviceType
The type of service.
[1] int : serviceState
The current state of the service.
[2] int : controlsAccepted
The controls the service accepts.
[3] int : win32ExitCode
The win32 error code for the service.
[4] int : serviceSpecificErrorCode
The service specific error code.
[5] int : checkPoint
The checkpoint reported by the service.
[6] int : waitHint
The wait hint reported by the service
服务状态实际上是第1个元素(从0开始计数)
写个函数包装一下,并附带上控制功能:
import win32service
import win32serviceutil
import win32api def GetSvcStatus(svcname):
svcstatusdic = {
#The service continue is pending.
win32service.SERVICE_CONTINUE_PENDING:"continue pending",
#The service pause is pending.
win32service.SERVICE_PAUSE_PENDING:"pause pending",
#The service is paused.
win32service.SERVICE_PAUSED:"paused" ,
#The service is running.
win32service.SERVICE_RUNNING:"running",
#The service is starting.
win32service.SERVICE_START_PENDING:"start pending",
# The service is stopping.
win32service.SERVICE_STOP_PENDING:"stop pending" ,
#The service is not running.
win32service.SERVICE_STOPPED:"stoped"
}
status = win32serviceutil.QueryServiceStatus(svcname)
if status:
return svcstatusdic.get(status[1],"unknown")
else:
return "error" svcname = "Spooler"
status = GetSvcStatus(svcname)
print("{} current status : {}".format(svcname,status))
if status == "running":
win32serviceutil.StopService(svcname)
else:
win32serviceutil.StartService(svcname)
win32api.Sleep(1000)
status = GetSvcStatus(svcname)
print("After Control,{} current status : {}".format(svcname,status))
输出结果如下:
掌握该模块的使用,对Windows平台的运维管理还是有相当帮助的。
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 ...
- 玩转Windows服务系列——命令行管理Windows服务
说到Windows服务的管理就不得不说通过命令行的方式管理Windows服务,因为无论是系统管理员,还是通过编程的方式调用cmd命令,命令行都是非常方便以及强大的工具. 接下来就看一下如何通过cmd命 ...
- 玩转Windows服务系列——命令行管理Windows服务
原文:玩转Windows服务系列——命令行管理Windows服务 说到Windows服务的管理就不得不说通过命令行的方式管理Windows服务,因为无论是系统管理员,还是通过编程的方式调用cmd命令, ...
- [转]玩转Windows服务系列——命令行管理Windows服务
本文转自:http://www.cnblogs.com/hbccdf/p/managewindowsservicewithcmd.html 说到Windows服务的管理就不得不说通过命令行的方式管理W ...
- 如何使用PowerShell管理Windows服务
[TechTarget中国原创] 作为一名系统管理员,最常见的任务之一就是学会管理Windows服务,这是保证Windows服务器和客户端正常运行的重要内容. 许多操作系统和应用程序都依赖于这些服务. ...
- 【python】使用python写windows服务
背景 运维windows服务器的同学都知道,windows服务器进行批量管理的时候非常麻烦,没有比较顺手的工具,虽然saltstack和ansible都能够支持windows操作,但是使用起来总感觉不 ...
- Python-windows服务-重启自动化
一. 前言 有了上一篇的“python初学”的基础,咱们就有了python的开发包,有了开发环境IDE,那我们就可以干活了.我的第一个选题就是让我们的windows服务可以按照我们的意愿进行自动重启. ...
- 第十三篇 一个安装、管理windows服务的桌面程序
在网上看到一个修改程序入口的程序去把windows 服务修改成控制台的程序,然后利用控制台的程序把服务安装和管理,也想起自己原来也写了一个对windows 报务管理的程序,不过是winform的. ...
随机推荐
- arcgis10.0 切片并发布服务及验证
1.切片参考网址:https://jingyan.baidu.com/article/fa4125accc6bef28ac7092d7.html 2.通过下面代码验证 参考网址https://www ...
- 数据挖掘、目标检测中的cnn和cn---卷积网络和卷积神经网络
content 概述 文字识别系统LeNet-5 简化的LeNet-5系统 卷积神经网络的实现问题 深度神经网路已经在语音识别,图像识别等领域取得前所未有的成功.本人在多年之前也曾接触过神经网络.本系 ...
- onvif 开发之video streamer---onvif实现功能和经验
目录(?)[-] 一产生onvif源码框架 从wsdl生成C头文件 从头文件生成源码框架 二创建soap运行环境 三RTSP视频对接 实现GetCapabilities命令 实现GetServices ...
- Ubuntu 12.04中文输入法的安装(zhuan)
Ubuntu 12.04中文输入法的安装 Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu ...
- nstall-Package : 无法找到程序包“MySql.Data.Entity.EF6”
在vs2013中用MySQL+EF6时,会遇到版本的问题,解决方法一般如下: 1 Install-Package EntityFramework -Version 6.0.0然后Enter2 Inst ...
- 九度OJ 1060:完数VS盈数 (数字特性)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5590 解决:2093 题目描述: 一个数如果恰好等于它的各因子(该数本身除外)子和,如:6=3+2+1.则称其为"完数" ...
- special points about git
1 about "origin/master tracks the remote branch" 1.1 what does tracking mean? after " ...
- Django框架打印orm转换过程中的sql_模型层
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console':{ 'level':'DEBU ...
- Eclipse使用方法和技巧二十六:浅谈快捷键
网络上到处都是eclipse有哪些经常使用的快捷键,当中还有非常多讲得着实不错.这里就不再狗尾续貂而是谈谈别的这段时间的一些思考.近期增加了开发团队.代码量突突的上去了.同一时候也发现 ...
- git创建与管理远程分支【转】
本文转载自:http://blog.chinaunix.net/uid-9398085-id-3164754.html git创建与管理远程分支 1.远程分支就是本地分支push到服务器上的时候产生的 ...