Bash script + program is terminated but an associated process is not

Hi!

I have created a bash script.

It started a program, then it runs.

If I end the program only the program is ended, but not 2 other subordinate program processes.

These do not close automatically.

Is there a possibility if I start my program from my bash script that all other program processes are also automatically closed?

I close the other 2 program processes with another bash script:

#!/bin/sh

pkill 'program name'

exit 0

Is there a way to add my EXIT pkill bash script to my bash script that starts my program?

Something like this:

#!/bin/sh

Run my program:
/path-to/program

After exit my program, execute pkill:
pkill 'program name'

exit 0

Please help, thanks!

have you tried pkill -15 -P ppid or even pkill -9 -P ppid ?

Hi pavlos_kairis,

How should I insert the command pkill -15 -P ppid or pkill -9 -P ppid into my bash script?

?

#!/bin/sh

/path-to/program

pkill -15 -P ppid

exit 0

?

The parent pid should be $$

What do you mean by that?

I just found out by accident that this is how it works with my bash script:

#!/bin/sh

/path-to/program

sleep 8m

pkill 'program name'

exit 0

When I close my program, my bash script continues to run.

I then press "Ctrl+c" in the mate terminal to
to end my bash script.

Not the best solution but the 2 other side program processes were also terminated.

the command would be, pkill -15 -P $$

$$ will resolve to the parent pid and pkill will kill all processes with that parent pid sending signal 15

and how should I include this in my bash script?

To which position.

Post times an example, thanks in advance.

Perhaps a better solution would be to use the trap built-in command of bash to do something when the shell script receives a signal or is about to exit for some other reason. Simply insert a line like this at the start of your script that launches another program which fails to clean up after itself, so to speak:

trap 'pkill "program name"' EXIT

So to add it to your original example, the script should look like this:

#!/bin/sh

trap "pkill 'program name'" EXIT

/path-to/program

exit 0

@ Gordon, very nice, wonderful, it works great with that.

Thanks a lot!

- - - S O L V E D - - -

1 Like