1
votes

I have the following piece of code, which works as expected. It ensures that 2 processes are always spawned, and if any process fails, the script comes to a halt. I have worked with GNU parallel earlier on simple one line scripts and they have worked really well.I'm sure the one below too can be made simpler.

The sleeper function in reality is MUCH more complex than one shown below.

The objective is that GNU parallel will call sleeper function in parallel and also do error handling

`sleeper(){
stat=$1
sleep 5
echo "Status is $1"
return $1
}

PROCS=2
errfile="errorfile"
rm "$errfile"
while read LINE && [ ! -f "$errfile" ]
do
    while [ ! -f "$errfile" ]
  do
     NUM=$(jobs | wc -l)
     if [ $NUM -lt $PROCS ]; then
            (sleeper $LINE || echo "bad exit status" > "$errfile") &
            break
     else
        sleep 2
     fi
  done
done<sleep_file
  wait`

Thanks

1

1 Answers

0
votes

What you are looking for is --halt (requires version 20150622):

sleeper(){
  stat=$1
  sleep 5
  echo "Status is $1"
  return $1
}
export -f sleeper
parallel -j2 --halt now,fail=1 -v sleeper ::: 0 0 0 1 0 1 0

If you do not want the sleeper to get killed (maybe you want it to finish so it cleans up), then use --halt soon,fail=1 to let the running jobs complete without starting new ones.