How to use subprocess to invoke "/etc/init.d/tomcat6/ stop" in Python
I want to invoke /etc/init.d/tomcat6 in subprocess. I have tried the below code, but it didn't work.
cmd="/etc/init.d/tomcat6/ stop"
p=subprocess.Popen(cmd)
stdout, stderr = p.communicate()
print s开发者_开发问答tdout,stderr
Anyone could help me, thanks.
Do it this way:
subprocess.call(['/etc/init.d/tomcat6', 'stop'])
or, if you really need to capture the standard output/error
p = subprocess.Popen(['/etc/init.d/tomcat6', 'stop'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
精彩评论