First of all:
What you describe as your desired answer is what you actually call: I also don't want to create a script that constantl polls. Inotify also is used in a constant loop to "listen" to changes in the current state. In other words: the other option you assume to exist does not exist without some kind of a loop.
I also think your image of a loop is too heavy. On every system, in every application there are numerous loops active to wait for triggers. It should have no noticable effect on your resources.
The "anatomy" of such a take-action-on-event mechanism is basically:
in a while loop:
check the current status;
if a is True:
take action A
if a is False:
do nothing
wait a few seconds
In a bash script
Integrating this script (to check your connection) in a while loop (with a few minor changes) should do what you describe: re-establish your connection and send a notification.
#!/bin/bash
while :
do
wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
:
else
rfkill block wifi
rfkill unblock wifi
notify-send "connection re-established"
fi
sleep 4
done
In a python script
The function check()
returns True or False. If True (connection is up), nothing happens. If False, your rfkill block wifi
/ rfkill ublock wifi
is executed.
#!/usr/bin/env python3
import socket
import time
import subprocess
def check():
try:
host = socket.gethostbyname("www.google.com")
s = socket.create_connection((host, 80), 2)
return True
except Exception:
return False
while True:
if check() == True:
pass
else:
subprocess.call(["/bin/bash", "-c", "rfkill", "block", "wifi"])
subprocess.call(["/bin/bash", "-c", "rfkill", "unblock", "wifi"])
subprocess.Popen(["/bin/bash", "-c", "notify-send 'conncection re-established'"])
time.sleep(4)
inotify
which seens to do something very similar to what i want: http://en.wikipedia.org/wiki/Inotify - haven't used it, dunno much about it though – vlad-ardelean Sep 21 '14 at 17:32sudo service network-manager restart
as mentioned here: http://askubuntu.com/a/399159/72216. If you want, I can make an example "watch" script. – Jacob Vlijm Sep 21 '14 at 18:34