I believe the simplest way is to start the program via a script and establish a trap
to kill
the whole process group:
#!/bin/bash
trap "kill -9 -- -$$" ERR EXIT
/path/to/gui-programm
How does it work?
A trap
is a shell builtin that execute a certain command if the script receives one (or more) signals. In this case EXIT stands for a graceful exit (i.e. closing the GUI programm) and ERR for any error (e.g. CTRL+C, or if foxit got killed by another reason). Ref: man signal
and man bash
The command in use is kill
, where -9
sends a SIGKILL. kill
may terminate a single process via kill <pid>
will act on a whole process group (i.e. process and all its children and forks) by adding a minus to the PID. --
is needed so that the following argument is not interpreted as a flag. I.e. kill -9 -- -$pid
will kill a while process group.
$$
is simply the PID of the current shell script. The GUI-program is a child of the script and also child processes of the GUI-program itself are part of the process group linked to the script as the origin.
If you want to test it, I suggest using many sleeps:
#!/bin/bash
#test script for kill via trap
trap "kill -9 -- -$$" ERR EXIT
for (( i=1 ; i<=100 ; i++ )) ; do
sleep 120
done
/path/to/gui-command
Now if you exit the GUI command, all sleeps will disappear. If you comment the trap part out, they will remain.
In order to ensure that the trap
is also executed on failures of child processes, it needs to be inherited via set -E
.
#!/bin/bash
set -E
trap "kill -9 -- -$$" ERR EXIT
/path/to/gui-programm