1

Consider this small bash function:

saySomethingToSomebody() {
    sleep 1
    echo "$1 $2." 
    sleep 2
}

When I call saySomethingToSomebody Hello Adam, it says Hello Adam. and waits a bit for letting him respond.

I want to parallelize it to greet the party guests more efficiently, but I also want to prevent a tragedy, so consider this new variant which uses GNU parallel (run by perl):

saySomethingToSomebody() {
    sleep 1
    if [ "$2" == "Brutus" ]; then
        echo "It's him! Brutus! Take care, he might kill someone!"
        killall -TERM perl # Stops parallel from starting new jobs, lets old ones finish
    else
        echo "$1 $2." 
        sleep 2
    fi
}; \
export -f saySomethingToSomebody; \
parallel --jobs=3 saySomethingToSomebody "Hello" ::: Adam Brutus Caesar Doris Emily Francis

I have two Cygwin installations. On one installation, it acts like I want it to: It sees Brutus and sends the SIGTERM to perl, the latter lets the other two greetings finish but doesn't start new jobs:

parallel: SIGTERM received. No new jobs will be started.
parallel: Waiting for these 3 jobs to finish. Send SIGTERM again to stop now.
parallel: saySomethingToSomebody Hello Caesar
parallel: saySomethingToSomebody Hello Adam
parallel: saySomethingToSomebody Hello Brutus
It's him! Brutus! Take care, he might kill someone!
Hello Adam.
Hello Caesar.

On a second Cygwin installation, it just doesn't output anything, so I think the greetings to Adam and Caesar have been killed by the SIGTERM.

Why is that happening? And what can I do about it?

1
  • Just to add it: Of course I do not really have a party with people like Caesar. And I know, Brutus might say Ey, you are about to issue a killall command! (And it's not March, so Caesar is safe for today!). It's an example, okay? ;-)
    – Bowi
    Commented Jul 2, 2021 at 8:09

1 Answer 1

1

The behaviour of GNU parallel got changed in the years passing between my two installations: Up to now, the signal to use for "stop parallel from starting new jobs, let the old ones finish" is SIGHUP and not SIGTERM any more.

killall -HUP perl

does the trick on the newer installation.

Information added by Ole Tange: The change happened in version 20190322.

3
  • The change happened in version 20190322: git.savannah.gnu.org/cgit/parallel.git/tree/NEWS#n666
    – Ole Tange
    Commented Jul 2, 2021 at 23:01
  • Also: you should probably use $PARALLEL_PID, so you do not kill another perl process.
    – Ole Tange
    Commented Jul 2, 2021 at 23:03
  • 1
    Thank you @OleTange, I've added your information. So you would propose using kill --signal SIGHUP $PARALLEL_PID instead?
    – Bowi
    Commented Jul 5, 2021 at 6:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .