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的. ...
随机推荐
- Jenkins--Run shell command in jenkins as root user?
You need to modify the permission for jenkins user so that you can run the shell commands. You can i ...
- EF6&EFCore 注册/使用实体类的正确姿势
首先回顾下EF中常规使用流程 1.新建实体类以及实体配置(data annotation或fluent api) [Table("Users")] public class Use ...
- 解决UICollectionView的Cell复用引起的布局混乱问题
解决UICollectionView的Cell复用引起的布局混乱问题 问题复现.gif 查了一下度娘发现没有好的解决办法,于是发动自己的聪明才智,终于找到如下解决办法(充分证明了自己动手丰衣足食啊
- c++ 系统函数实现文件拷贝
#include "stdafx.h" #include <string> #include<windows.h> #include<iostream ...
- iOS开发 僵尸调试
本文转载至 http://blog.sina.com.cn/s/blog_a843a8850101dxin.html 引自:http://blog.csdn.net/likendsl/articl ...
- 一步一步学ios UITextView(多行文本框)控件的用法详解(五5.8)
本文转载至 http://wuchaorang.2008.blog.163.com/blog/static/48891852201232014813990/ 1.创建并初始化 创建UIText ...
- 【oracle案例】ORA-01102: cannot mount database in EXCLUSIVE mode
ORA-01102: cannot mount database in EXCLUSIVE mode 今天在fedora上安装完10g后,测试数据库是否安装成功.STARTUP数据库时,发生如下错误: ...
- 九度OJ 1026:又一版 A+B (进制转换)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:11412 解决:3086 题目描述: 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < ...
- 远程服务器上的weblogic项目管理(五) PermGen内存溢出问题
weblogic偶尔会出现PermGen异常,内存溢出的问题,这个时候需要修改weblogic安装目录下的domain/common/bin/commEnv.cmd. 打开后在其中找到: set ME ...
- 采集练习(十) php 获得电视节目预告---数据来自搜视网
前几天逛湖南卫视,偶然间发现它的网站上也有节目预告,一看源码,居然是来自搜视网的xml,于是就想获得它的数据(页面直接ajax加载估计会有跨域问题) 前阵子也写过另一个方法获得 节目预告(采集练习(七 ...