#[Solved] Terminating children subprocesses on exit in bash.

15 messages · Page 1 of 1 (latest)

rain jungle
#

Hey im writing a bash script that launches a bunch of subprocesses, and when the process of the script is terminated all subprocesses and possible children of those subprocesses will be terminated

#!/bin/bash
trap "kill \$(pgrep -P $$)" EXIT

command0 &
command1 &
etc...

wait

This works fine when i run the script by itself. Children processes are propperly cleaned up.
However, when running this script as a subprocess itself:

./self_terminating.bash &
sleep 10 # let subprocesses do their thing
kill $!

Children processes are not killed propperly.
Some good samaritan wanna lend me hand?

wind wren
rain jungle
#

isn't it the same??

wind wren
#

...you captured the subprocess ID as you created them, and added them to some Data-Strukture which your trap function has access to and iterated trough to kill all the child-processes? Yes?

rain jungle
#

oh wow thanks i wouldn't have gotten it without all that condescendence ❤️

#

no shit people talk shit about the arch community

wind wren
#

Multilingual autocorrect fucked me up there, fixed it

wind wren
#

Man, chill
I just asked that as a sanity check

nova temple
#

... solved

cobalt sorrelBOT
#

#9742 📣 If there is nothing else that we can do for you, please close this thread by adding "[SOLVED]" to the title.

  • You can do this on desktop by clicking the three dots on the top right of the page, and pressing "Edit Post".
  • You can also do this on Android / iOS by long tapping your post in the text channel list, and tapping "Edit Post".

Your title should be like so: https://cdn.discordapp.com/attachments/503224329191030787/1039841700417384498/image.png
Thank you

rain jungle
#

sorry

#

fr