I am using gnu parallel in a subshell function, e.g.,

    parallel --will-cite sleep ::: 60

upon sending SIGTERM to the function I want parallel to be killed too. But I get

$ func &
[1] 13255
$ pgrep -P 13255 # child of func -> the subshell
$ pgrep -P 13256 # child of the subshell -> gnu parallel
$ kill -TERM -- -13255 # terminating the process group
parallel: SIGTERM received. No new jobs will be started.
parallel: Waiting for these 1 jobs to finish. Send SIGTERM again to stop now.
parallel: sleep 60
$ kill -TERM -- -13255 # sending a second SIGTERM
bash: kill: (-18093) - No such process

so I cannot kill parallel without first tracking its process id. I tried to put a trap inside the function

    trap "pkill -P $$; pkill -P $$; exit" TERM
    parallel --will-cite sleep ::: 60

where pkill -P $$ should kill all children, but checking ps $pid_of_parallel revealed, parallel keeps running after the first termination signal.

My goal is to write the function such that funct &; kill $! will terminate it and all children immediately. How can I achieve this?


I believe the newest version of GNU parallel includes this now. From the GNU parallel development list:

On Sun, Mar 10, 2019 at 8:29 PM Ole Tange wrote:

To stop GNU Parallel today you need to send TERM to make it stop starting new jobs followed by another TERM to kill the running jobs.

I am considering changing that to send HUP to stop starting new jobs and TERM to kill running jobs.

This will make it easier to kill GNU Parallel:

$ bash -c 'parallel -j1 sleep ::: 111 222' &

This would then kill the process group with bash and parallel

$ kill -TERM -$!

It will make it incompatible with previous versions.

So the newest version includes this. Please, please, please see if you can break that.

Can you show an example where the new behavior is worse than the old?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.