首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rsync退出值

rsync退出值
EN

Stack Overflow用户
提问于 2013-01-24 02:56:33
回答 1查看 1.7K关注 0票数 2

我在rsync手册中读到rsync返回0- 35范围内退出值。

我尝试使用以下代码捕获rsync的输出:

代码语言:javascript
复制
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的以下输出:

代码语言:javascript
复制
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]

我的问题:

  • Is the rsync output is only as text (如上所述)?
  • 如何区分成功(代码0)和错误?

(假设:如果成功,p.stdout的长度将为零,其他任何内容都是失败的,这样的假设正确吗?)

EN

回答 1

Stack Overflow用户

发布于 2013-01-24 03:00:20

您可以通过returncode属性访问Popen对象进程的返回码。如果它的值为None,则说明该过程尚未完成;请稍后再检查。

例如:

代码语言:javascript
复制
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"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14487249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档