Python重启Windows服务

时间:2015-02-19 21:23:02

标签: python

快速提问:

我有一个将重启Windows服务的python脚本:

import os 

import win32serviceutil

serviceName = "Apple Mobile Device"


win32serviceutil.StopService(serviceName)

我需要添加其他几项服务。

我该怎么做?

THX

1 个答案:

答案 0 :(得分:7)

如果您想在每次执行以下操作时手动输入它们:

import os 
import win32serviceutil

stopping = 1
while stopping == 1:
    service_name = raw_input('enter the name of the service[s] you would like to stop\nor enter done or exit to exit\n: ')
    if service_name.lower() == 'done' or 'exit':
        stopping = 2
    else:
        try:
            win32serviceutil.StopService(service_name)
            print '{} stopped'.format(service_name)
        except:
            print 'could not stop service {}'.format(service_name)

如果您想在每次运行该功能时自动执行此操作,请执行更多类似 payhima 的操作:

import wmi
import os 
import win32serviceutil



service_name = ['this is service one  :)'
                ,'service 2'
                ,'service 1 million'
                ]
for s in service_name:
    win32serviceutil.StopService(s)

否则你可以列出所有服务并通过关闭它们来崩溃你的机器:)