我在rsync手册中读到rsync返回0- 35范围内退出值。
我尝试使用以下代码捕获rsync的输出:
import subprocess, time, os, sys
cmd = ["rsync", "-avuxz", "/etc/passwd" ,"/tmp1/"]
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in p.stdout:
print line.rstrip()使用上面的程序,我捕获了rsync的以下输出:
rsync: mkdir "/tmp1" failed: Permission denied (13)
rsync error: error in file IO (code 11) at main.c(587) [Receiver=3.0.9]
rsync: connection unexpectedly closed (9 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(605) [sender=3.0.9]我的问题:
(假设:如果成功,p.stdout的长度将为零,其他任何内容都是失败的,这样的假设正确吗?)
发布于 2013-01-24 03:00:20
您可以通过returncode属性访问Popen对象进程的返回码。如果它的值为None,则说明该过程尚未完成;请稍后再检查。
例如:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# Dumb way to wait for the process to complete
while p.returncode is None:
os.sleep(1)
if p.returncode:
print "Error, rsync exited with non-zero status"
else:
print "Success, rsync exited with zero status"https://stackoverflow.com/questions/14487249
复制相似问题