Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get exit code when using xargs (parallel)

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

#! /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.

Anyone knows?

like image 228
bLuEdDy Avatar asked Apr 14 '14 08:04

bLuEdDy


People also ask

How do I exit xargs command?

-x, --exit Exit if the size (see the -s option) is exceeded. --help Print a summary of the options to xargs and exit. --version Print the version number of xargs and exit. The options --max-lines (-L, -l), --replace (-I, -i) and --max- args (-n) are mutually exclusive.

Does xargs work in parallel?

xargs will run the first two commands in parallel, and then whenever one of them terminates, it will start another one, until the entire job is done. The same idea can be generalized to as many processors as you have handy. It also generalizes to other resources besides processors.

How does xargs command work?

The xargs command builds and executes commands provided through the standard input. It takes the input and converts it into a command argument for another command. This feature is particularly useful in file management, where xargs is used in combination with rm , cp , mkdir , and other similar commands.

What is the default command used by xargs?

If no command is specified, xargs executes echo by default. You many also instruct it to read data from a file instead of stdin.


1 Answers

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:

  • 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 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.
like image 54
Austin Phillips Avatar answered Sep 28 '22 04:09

Austin Phillips