开发者

How to use subprocess to invoke "/etc/init.d/tomcat6/ stop" in Python

开发者 https://www.devze.com 2023-01-20 06:42 出处:网络
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\"

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()
0

精彩评论

暂无评论...
验证码 换一张
取 消