[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Making --fg return the exit code of the command

From: Samuel Thibault
Subject: [PATCH] Making --fg return the exit code of the command
Date: Sun, 30 Jan 2022 16:52:56 +0100
User-agent: NeoMutt/20170609 (1.8.3)


GNU parallel is doing almost exactly what I need, except that it does
not return the exit code of the command, but rather only 0/1.

Basically the kind of thing I am after is this kind of thing:

for algo in algo1 algo2 algo3
        for size in $(seq 1 10)
                        sem --fg -j $J ./run_test $TEST
                        if [ $RET = 77 ] ; then
                                echo "$TEST" skipped
                        elif [ $RET = 0 ] ; then
                                echo "$TEST" succeeded
                                echo "$TEST" failed
                        exit $RET
                ) &

while true
        wait -n
        if [ $RET = 127 ]; then break; fi
        if [ $RET != 0 -a $RET != 77 ]; then RESULT=1 ; fi

exit $RESULT

And several of these are getting run in parallel etc. so it's a
parallelism mess. Using parallel --semaphore nicely allows to let all of
these use a given level of parallelism.

I know that parallel has functionalities to gather results etc. but the
example above is just a very simplified case of what I need (various
levels of loops, different levels of "I want to know whether this set of
runs failed or not", etc.), and as is shown we want to handle 77 (skip)
specially, so I need to keep the general shape of the code and not rely
on parallel for that part.

It happens that the only thing I currently miss is that I never get 77,
parallel --semaphore --fg always turns it into 1.

And it happens that it seems to be easy to get that behavior, see the
attached patch. The idea is that when using --fg, the situation is
similar to --halt now,fail=1: we have just one job to report the result
of, so we can just return its exit code. I added an --fg-exit option to
do that.

I would even argue that this should be the default of --fg:
parallel_tutorial says itself: “The difference between this [sem
--fg] and just running the command, is that a mutex is set, so if other
@strong{sem}s were running in the background only one would run at a
time.”  While reading that I was assuming that sem --fg would just
return the exit code of the command and was very surprised to see that
it does not. So we could even simplify my patch into not introducing an
fg-exit option, and just test for $opt::fg.

With regards,

Attachment: patch
Description: Text document

reply via email to

[Prev in Thread] Current Thread [Next in Thread]