如何在使用xargs(并行)时获取退出代码

时间:2022-09-24 01:07:59

I'd made a script for launching parallel rsync process:

我做了一个启动并行rsync进程的脚本:

#! /bin/bash
LIST=$1
DEST_DIR=$2
RSYNC_OPTS=$3
#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进程的退出状态。我知道有可能获得一个pipestatus数组,但我需要捕获退出代码以了解rsync是否成功。

Anyone knows?

有谁知道?

1 个解决方案

#1


8  

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.
  • 让xargs生成的进程打印其退出代码并让父任务解析所有退出代码输出以确定每个rsync的退出代码。
  • Use GNU parallel with 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 after parallel exits to determine which rsync commands failed and their respective error codes.
  • 将GNU parallel与--joblog选项一起使用。这将创建一个文件,其中包含与其退出代码和其他信息并行运行的所有命令。然后可以在并行退出后解析此文件,以确定哪些rsync命令失败以及它们各自的错误代码。

#1


8  

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.
  • 让xargs生成的进程打印其退出代码并让父任务解析所有退出代码输出以确定每个rsync的退出代码。
  • Use GNU parallel with 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 after parallel exits to determine which rsync commands failed and their respective error codes.
  • 将GNU parallel与--joblog选项一起使用。这将创建一个文件,其中包含与其退出代码和其他信息并行运行的所有命令。然后可以在并行退出后解析此文件,以确定哪些rsync命令失败以及它们各自的错误代码。