bug-parallel
[Top][All Lists]
Advanced

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

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


From: Samuel Thibault
Subject: Re: [PATCH] Making --fg return the exit code of the command
Date: Sun, 13 Feb 2022 16:44:55 +0100
User-agent: NeoMutt/20170609 (1.8.3)

Ping?

Samuel

Samuel Thibault, le dim. 30 janv. 2022 16:52:56 +0100, a ecrit:
> Hello,
> 
> 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
> do
>       for size in $(seq 1 10)
>       do
>               (
>                       TEST=${algo}_${size}
>                       sem --fg -j $J ./run_test $TEST
>                       RET=$?
>                       if [ $RET = 77 ] ; then
>                               echo "$TEST" skipped
>                       elif [ $RET = 0 ] ; then
>                               echo "$TEST" succeeded
>                       else
>                               echo "$TEST" failed
>                       fi
>                       exit $RET
>               ) &
>       done
> done
> 
> RESULT=0
> while true
> do
>       wait -n
>       RET=$?
>       if [ $RET = 127 ]; then break; fi
>       if [ $RET != 0 -a $RET != 77 ]; then RESULT=1 ; fi
> done
> 
> 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.
> 
> Put another way,
> 
> sem --fg exit 42
> 
> should return 42, not 1.
> 
> With regards,
> Samuel

> diff --git a/src/parallel b/src/parallel
> index 9ea69033..b6306cbd 100755
> --- a/src/parallel
> +++ b/src/parallel
> @@ -115,7 +115,10 @@ sub halt() {
>       }
>       wait_and_exit($Global::halt_exitstatus);
>      } else {
> -     wait_and_exit(min(undef_as_zero($Global::exitstatus),101));
> +     if(not defined $Global::halt_exitstatus) {
> +         wait_and_exit(min(undef_as_zero($Global::exitstatus),101));
> +     }
> +     wait_and_exit($Global::halt_exitstatus);
>      }
>  }
>  
> @@ -1696,6 +1699,7 @@ sub options_hash() {
>        "semaphoretimeout|st=s" => \$opt::semaphoretimeout,
>        "semaphorename|id=s" => \$opt::semaphorename,
>        "fg" => \$opt::fg,
> +      "fg-exit" => \$opt::fg_exit,
>        "bg" => \$opt::bg,
>        "wait" => \$opt::wait,
>        # Shebang #!/usr/bin/parallel --shebang
> @@ -11101,6 +11105,14 @@ sub set_exitsignal($$) {
>           $limit = $Global::total_completed;
>       }
>       if(not defined $limit) {
> +         if($opt::fg and $opt::fg_exit and not defined 
> $Global::halt_exitstatus) {
> +             # --fg-exit
> +             # Emulate Bash's +128 if there is a signal
> +             $Global::halt_exitstatus =
> +                 ($job->exitstatus()
> +                  or
> +                  $job->exitsignal() ? $job->exitsignal() + 129 : 0);
> +         }
>           return ""
>       }
>       #  --halt # => 1..100 (number of jobs failed, 101 means > 100)
> diff --git a/src/parallel.pod b/src/parallel.pod
> index 2e2c6424..0bac31c8 100644
> --- a/src/parallel.pod
> +++ b/src/parallel.pod
> @@ -908,7 +908,18 @@ foreground (opposite B<--bg>), and wait for completion 
> of the command
>  before exiting.
>  
>  
> -See also: B<--bg> B<man sem>
> +See also: B<--fg-exit> B<--bg> B<man sem>
> +
> +
> +=item B<--fg-exit>
> +
> +Return exit code of command.
> +
> +Along with B<--fg> the exit value of GNU B<parallel> will be that of
> +the command being run.
> +
> +
> +See also: B<--fg-exit> B<--bg> B<man sem>
>  
>  
>  =item B<--fifo>



reply via email to

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