I am using gnu parallel in a subshell function, e.g.,
func()(
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
13256
$ pgrep -P 13256 # child of the subshell -> gnu parallel
13257
$ 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
func()(
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?