0

So, my situation is this: I have created a launcher, key bindings and an alias that opens GLava and CMus in a drop-down terminal at the same time, using this shell script:

 #!/bin/bash c=$(ps -e | grep -c xfce4-terminal) if [ $c -gt 0 ] then xfce4-terminal --tab --drop-down -x cmus | glava --desktop & $1 else xfce4-terminal --drop-down -x cmus | glava --desktop & $1 fi 

It is perfect and glorious, but for one small problem. I'd very much like for both programs to also CLOSE at the same time. Is there a way to have one programs' termination trigger another? Specifically, if I have CMus and GLava open, I'd like to be able to close CMus (say, by simply pressing 'q' and quitting CMus or by closing the terminal, or killing the program, OR a special key binding, etc.) and have GLava immediately close/terminate as well. As it stands, I have a separate key binding to pkill GLava after I've closed CMus, which works fine, I suppose, but is certainly a little clunkier than I'd like. Maybe there's a way for GLava to only get triggered if CMus is running, a sort of whitelist maybe? A simple shell that closes both programs that I can bind? Or another thought perhaps? I'm open to any suggestions, being the noob that I am. Any help is much appreciated.

2 Answers 2

1

A simple way to let processes communicates are signals.

bash have builtin called trap that let you add an handler for a given signal, so you could wrap all you command inside another script and set such handler to kill all the other programs when this receive a given signal.

Consider:

cat proc_a.sh #!/bin/bash echo "Load program 1 as : $$" sleep 1d cat proc_b.sh #!/bin/bash echo "Load program 2 as : $$" sleep 1d 

Now build a wrap to launch the other two process:

cat wrap.sh #!/bin/bash ./proc_a.sh a_pid=$! ./proc_b.sh b_pid=$! cleanup() { echo "Kill process a $a_pid" kill $a_pid echo "Kill process b $b_pid" kill $b_pid echo "Kill me : $$" kill $$ } trap cleanup SIGINT while :;do sleep 1d done 

if you launch ./wrap.sh and then send a SIGINT (Press CTRL-C) You can see this:

Load program 1 as : 27141 Load program 2 as : 27142 ^CKill a process a 27141 kill process b 27142 Kill me : 27140 Terminato 

The drawback with this is that ANYTIME you close one process also the other will be closed, apart from the fact that from now you need to kill the wrap instead of one of a process.

To avoid this drawback you can launch the xfce program from insdie a script and set a signal handler directly here, when one of the two receive a closing signal search the pid of the other xfce-wrapper-program and kill this.

4
  • This is very close to what I'm looking for. I think. It's honestly a bit over my head. I'm not even sure how I would customize those two scripts. I would need the two programs specific PID's, correct? And does the first script replace the one I'm already using? Also, I've edited my question to reflect my more specific wants/needs. Commented May 25, 2021 at 9:31
  • 1
    Each of the two program need to be wrapped inside a script, then set an handler as in the exaple that perform the kill operation on the pid of the other program you ahve launched. Or you can take a look to terminal multiplexing. See tmux or screen Commented May 25, 2021 at 10:04
  • No, I don't think terminal multiplexing will do in this particular instance, as GLava is a music visualizer that embeds into the desktop. Having it run in a terminal in the foreground wouldn't be ideal. Unless, I'm confused as to how you're suggesting I use the multiplexer, which is entirely possible! 😋 And just to be clear, you're saying I'd have to write -two- or even -three- additional scripts just to get this concept working? Commented May 25, 2021 at 10:43
  • Based on your edit: say, by simply pressing 'q' and quitting CMus or by closing the terminal, or killing the program, OR a special key binding, etc. make me think to terminal multiplexer and some key bindings to make what you want happen. Commented May 25, 2021 at 10:52
0

So, I finally got around to giving this a try, and as I'm sure you know, the script idea & execution are solid. But unfortunately it's not what I'm looking for. What I exactly want is something, probably a script, that will trigger the closure of a secondary, specific program when I close the first specific program. So, say I'm done listening to music in CMus. I'd like to be able to close down that program and have that trigger the secondary program to the also close down. Sort of like piping, but for program closure. Is this possible?

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.