0

I want to launch multiple applications (e.g. 3 PDF viewers) in a manner that, if you are done and close one of the windows, all of the applications launched together all close as well. What is the best way to implement this?

0

The first solution for this I can think up off the time of my head is this: open all 3 programs in a terminal, like so: program & program & program &. Then, when you close the terminal, the sub-processes will close, too.

The second thing I thought of is programming a shell script to terminate child processes when there are fewer than three of them. Here's a script you can try:

#!/bin/sh

program &
program &
program &
while [ $(jobs | wc -l) -gt 2 ]; do sleep 2; done
kill $(jobs -p)
exit

Do note I have not tested this.

2
  • Good suggestion, and thanks! But it maybe a bit unwieldy to have a terminal by the side to close the windows altogether. Was looking for something that is more automatic? Checking very 2 seconds or so also seems a bit wasteful too, no? – d.r Jun 12 '20 at 19:08
  • That's where my 2nd answer comes in handy: you don't necessarily need a terminal to run a shell script. If you add this to a *.desktop file, you can avoid having a terminal at all. That said, if you don't have that sleep command, the shell will be CONSTANTLY checking for the presence/absences of jobs. This will use an entire CPU core just for nothing (bad for performance and battery life). That said, you could change "2" to pretty much any value (with or without decimal a point), so feel free to tweak that number as desired. – TSJNachos117 Jul 19 '20 at 6:02

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.