bash 使用 xargs 时如何获取退出代码(并行)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23055708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get exit code when using xargs (parallel)
提问by bLuEdDy
I'd made a script for launching parallel rsync process:
我制作了一个用于启动并行 rsync 进程的脚本:
#! /bin/bash
LIST=
DEST_DIR=
RSYNC_OPTS=
#echo "rsyncing From=$SRC_DIR To=$DEST_DIR RSYNC_OPTS=$RSYNC_OPTS"
echo $LIST|xargs -n1 -d, echo|xargs -n1 -P 0 -I% rsync --rsync-path='sudo rsync' ${RSYNC_OPTS} % ${DEST_DIR}
Then, I have problems to get the exit status of the rsync process. I know that is possible to get an array of pipestatus, but I need to catch the exit code to know if the rsync was made successfully or not.
然后,我在获取 rsync 进程的退出状态时遇到了问题。我知道可以获取管道状态数组,但是我需要捕获退出代码以了解 rsync 是否成功。
Anyone knows?
有谁知道?
采纳答案by Austin Phillips
The man page for xargs shows the possible exit status values, however it can only produce a single aggregated exit code, not an exit code per child that it runs. You could try one of these options:
xargs 的手册页显示了可能的退出状态值,但是它只能生成一个聚合退出代码,而不是它运行的每个子进程的退出代码。您可以尝试以下选项之一:
- Have the process that xargs spawns print its exit code and have the parent task parse all the exit code outputs to determine the exit code for each rsync.
- Use GNU parallelwith the
--joblog
option. This will create a file containing all the commands which were run in parallel along with its exit code and other information. This file could then be parsed afterparallel
exits to determine which rsync commands failed and their respective error codes.
- 让 xargs 产生的进程打印其退出代码,并让父任务解析所有退出代码输出以确定每个 rsync 的退出代码。
- 使用GNU平行与
--joblog
选项。这将创建一个文件,其中包含所有并行运行的命令及其退出代码和其他信息。然后可以在parallel
退出后解析此文件以确定哪些 rsync 命令失败及其各自的错误代码。