Python 子进程返回码 - 不同的结果
我来在生产机器上运行一个脚本,但它在第一次检查中失败了。我已经将范围缩小到我的“ping”检查(由子进程调用)返回非零结果的事实。
这是最简单形式的代码:
import subprocess
import sys
ip="127.0.0.1"
ping = subprocess.Popen(["ping", "-c", "2", "-w", "1", ip], shell=False)
ping.wait()
if ping.returncode != 0:
print ping.returncode, "ERROR: failed to ping host. Please check."
sys.exit(1)
else:
print "OK"
两台服务器都是 CentOS 5.5 和 CentOS 5.5。 Python 2.4.3,但一种可以工作,另一种则不行!
在两种情况下,从命令行手动 ping(127.0.0.1)都会返回 0。为什么在 python 中使用 subprocess 模块时它们有区别?
干杯。
I came to run a script on a production box, and it failed in one of it's first checks. I've narrowed it down to the fact my "ping" check (invoked by subprocess) is returning a non-zero result.
Here's the code in it's simplest form:
import subprocess
import sys
ip="127.0.0.1"
ping = subprocess.Popen(["ping", "-c", "2", "-w", "1", ip], shell=False)
ping.wait()
if ping.returncode != 0:
print ping.returncode, "ERROR: failed to ping host. Please check."
sys.exit(1)
else:
print "OK"
Both servers are CentOS 5.5 & Python 2.4.3, but one works and one doesn't!
From the command line a manual ping (of 127.0.0.1) returns 0 in both cases. Why is their a difference when using the subprocess module in python?
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论