无法通过python中的子进程启动和停止服务

时间:2017-05-25 07:25:53

标签: python

我无法通过子程序在python中启动和停止服务。得到像

这样的错误
Traceback (most recent call last):
File "subProcessExample.py", line 3, in <module>
p = Popen(["C:/Program Files/Apache Software Foundation/Tomcat8.0/bin", service, "stop"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
File "C:\Python27\lib\subprocess.py", line 679, in __init__  
   errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 893, in _execute_child 
 startupinfo) 
WindowsError: [Error 5] Access is denied

1 个答案:

答案 0 :(得分:0)

我解决了我的错误如下

1.我更改了扩展名为.exe的服务名称 2.我以管理员身份运行cmd

以下是启动和停止服务的代码

# To stop service
from subprocess import Popen, PIPE
path = "C:/Program Files/Apache Software Foundation/Tomcat 8.0/bin/"
service = "Tomcat8.exe"
def startService(service, path):
 p = Popen([path+service, "start"])
 stdout, stderr = p.communicate()
 if(p.poll() == 0):
     print "SUCCESS"
 else:
    print "Failed to start"
startService(service, path)

#To stop service replace "start" with "stop"