Search code examples
bashshellreturn-valuegnu-parallel

how to forward the return value of a command run through gnu parallel


I have the following command

find $build -name "tester_*" -type f -executable | parallel "valgrind --tool=memcheck --leak-check=full --show-reachable=yes --track-origins=yes --error-exitcode=1 {} &> $build/memcheck.{/}.log"

the above command will run valgrind in parallel for all tester_ executables I have in my project build directory.

if one of those tests fail, execution for all will stop and the return value of the failed test will cause parallel to fail. This failed return value can thus be intercepted and possibly reported to the user.

the problem is that due to the amount of tests that are run, it would be convenient if I can be notified about whic test failed. Using "||" (or) in bash could help along with echo, like so:

find $build -name "tester_*" -type f -executable | parallel "valgrind --tool=memcheck --leak-check=full --show-reachable=yes --track-origins=yes --error-exitcode=1 {} &> $build/memcheck.{/}.log || echo {} failed"

Unfortunately now the echo command will return success and parallel will continue with its execution. Is it possible to somehow propagate this error even though "echo" will be executed ?


Solution

  • You can use

    find ... | parallel "valgrind ... || { echo {} failed; exit 1; }"
    

    This will print ... failed and then exit with status 1 if valgrind failed.

    If the actual exit code is important you have to store the exit code.

    find ... | parallel "valgrind ... || { err=$?; echo {} failed; exit $err; }"
    

    For the rare case in which find lists a file named -n or something similar it would be better to use printf '%s failed\n' {} instead of echo {} failed.