Set networking so as to turn Wi-Fi on/off when X process is started/interrupted (Linux)

2

0

My aim is to set (through scripts/daemons/whatever piece of software might help) my laptop networking so as to turn on automatically when I start software X (e.g. Firefox), and automatically turn off once that software has been closed/terminated.
(even more ambitious, setting 2 softwares X and Y as switches, if possible)
Any contribution will be greatly appreciated!

ed0

Posted 2018-05-17T11:40:18.133

Reputation: 23

1Simplest way: write a script that wraps calling of X, adjusts networking before calling it, monitors X for termination, and adjusts networking back. Call script instead of X. Doesn't work if you plan to use several instances of X simultaneously. In case this is an XY question: If you want to give Firefox a different networking environment, you can also call it inside a network namespace that's properly set up. – dirkt – 2018-05-17T12:41:59.253

Thank you very much @dirkt! If you could write an example of script code as an answer, that would be it for me. I am using elementary OS, so my base is Ubuntu – ed0 – 2018-05-17T14:37:36.830

Answers

1

Commands for stopping and starting networking are :

sudo /etc/init.d/networking { stop | start }
sudo nmcli { networking | radio } { on | off }

Or for just one particular network interface :

sudo ifdown ethX
sudo ifup ethX

The last one may have a problem if your router is not in /etc/sysconfig/network-scripts/route-ethX, then it may not be completely deleted/added by the ifup/ifdown scripts. For example, to delete you may also need to use :

route del default ethX

After launching firefox, to wait for it to finish you can use the wait command with or without parameters.

This is the script which worked for the poster :

nmcli radio wifi on
sleep 2
firefox
nmcli radio wifi off

Sleep is done with the number of seconds to wait before the network stabilizes, empirically tested.

You should also ensure that the network is disabled after boot.

harrymc

Posted 2018-05-17T11:40:18.133

Reputation: 306 093

thank you very much! I have made a couple of adjustments to your script, and it works exactly as intended. This is the final result – ed0 – 2018-05-20T13:14:01.053

nmcli radio wifi on / sleep 2 / firefox / wait firefox / nmcli radio wifi off – ed0 – 2018-05-20T13:14:27.257

as soon as you have updated your answer, I'll award you the bounty points! – ed0 – 2018-05-20T13:15:42.137

Done as requested. – harrymc – 2018-05-20T13:28:05.600

firefox doesn't return until it's closed, so you don't need wait. moreover, wait: \firefox': not a pid or valid job spec`. – guest-vm – 2018-05-20T17:29:12.427

I was wondering on which Linux distribution wait firefox worked ... But wait doesn't hurt here, just in case. – harrymc – 2018-05-20T17:57:16.710

My fault for not realizing! "wait" can totally be omitted, has no job in there!! – ed0 – 2018-05-20T22:14:17.947

Well, in that case, out it goes! – harrymc – 2018-05-21T06:19:04.040