简体   繁体   English

如何使用具有管理员权限的子进程停止和启动Windows服务?

[英]How can i stop and start windows services using subprocess with admin permissions?

I am building a python tool to update an application. 我正在构建一个python工具来更新应用程序。 To do so i have to stop the apache service, do some update related stuff and then start it again, after the update ist finished. 为此,我必须停止apache服务,执行一些与更新相关的工作,然后在更新ist完成后再次启动它。

Im currently using python 3.7.2 on Windows 10. 我目前在Windows 10上使用python 3.7.2。

I have tried to somehow build a working process using these questions as a reference: 我试图通过使用这些问题作为参考来建立工作流程:

Run process as admin with subprocess.run in python 在python中使用subprocess.run以管理员身份运行进程

Windows can't find the file on subprocess.call() Windows在subprocess.call()上找不到文件

Python subprocess call returns "command not found", Terminal executes correctly Python子进程调用返回“找不到命令”,终端正确执行


def stopApache():
    processName = config["apacheProcess"]
    #stopstr = f'stop-service "{processName}"'
    # the line abpove should be used once im finished, but for testing 
    # purposes im going with the one below
    stopstr = 'stop-service "Apache2.4(x64)"'
    print(stopstr)
    try:
        subprocess.run(stopstr, shell=True)
        #subprocess.run(stopstr)
        # the commented line here is for explanatory purposes, and also to 
        # show where i started.
    except:
        print('subprocess failed', sys.exc_info())
        stopExecution()

From what i have gathered so far, the shell=TRUE option, is a must, since python does not check PATH. 根据到目前为止的经验, shell=TRUE选项是必须的,因为python不检查PATH。 Given the nature of what im trying to do, i expected the service to get stoppped. 鉴于我试图做的事情的性质,我希望这项服务能够停止。 In reality the console error looks like this : 实际上,控制台错误如下所示:

stopstr =  get-service "Apache2.4(x64)"
Der Befehl "get-service" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

Which roughly translates to : the command "stop-service" is either incorrectly spelled or could not be found. 大致翻译为:命令“ stop-service”的拼写错误或找不到。

If i run the same command directly in powershell i get a similar error. 如果我直接在Powershell中运行相同的命令,则会收到类似的错误。 If i open the shell as admin, and run i again, everything works fine. 如果我以管理员身份打开外壳程序,然后再次运行我,则一切正常。

But, if i use the very same admin shell to run the python code, i am back to square one. 但是,如果我使用完全相同的管理外壳来运行python代码,那么我将回到正题。

What am i missing here, aparently there is some issue with permissions but i cannot wrap my head arround it 我在这里想念的是什么,显然权限有问题,但是我无法解决这个问题

The command for stopping a service in MS Windows is net stop [servicename] 在MS Windows中停止服务的命令是net stop [servicename]

So change stopstr to 'net stop "Apache2.4(x64)"' 因此将stopstr更改为'net stop "Apache2.4(x64)"'

You will need to run your script as admin. 您将需要以管理员身份运行脚本。

shell=True runs commands against cmd, not powershell. shell=True针对cmd(而非powershell)运行命令。

Powershell is different to the regular command line. Powershell与常规命令行不同。 So to get stop-service to work you'd have to pass it to an instance of powershell. 因此,要使stop-service正常工作,您必须将其传递给powershell实例。

stopstr = 'stop-service \\"Apache2.4(x64)\\"'
subprocess.run(['powershell', stopstr], shell=True)

As you noted in a comment, it's neccessary to escape the " around the service name, as first python will unescape them, then powershell will use them. 正如您在评论中指出的那样,必须在服务名称周围转义" ,因为第一个python将取消转义它们,然后powershell将使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM