Python重启windows服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28617173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:31:10  来源:igfitidea点击:

Python restarting windows services

python

提问by zooted

Quick question:

快速提问:

I have a python script that will restart a windows service:

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

import os 

import win32serviceutil

serviceName = "Apple Mobile Device"


win32serviceutil.StopService(serviceName)

I need to add several other services.

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

How would I do that?

我该怎么做?

Thx

谢谢

回答by Yojimbo

If you want to manually enter them each time do something like this:

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

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)

elif you want to do it automagically every time you run the function do something more like paidhima:

el如果您想在每次运行该函数时自动执行此操作,请执行更像paidhima 的操作

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)

else you could list all the services and crash your machine by shutting them all down :)

否则,您可以列出所有服务并通过将它们全部关闭来使您的机器崩溃:)