How do I run a simple Zenity script after disconnecting from WiFi?
I am running Kubuntu 12.10 64bit. I am trying to get a bash script to execute when I am disconnected from the network. I created a file in the folder /etc/network/if-down.d/
called test
which has a single line:
zenity --info --text="network down!"
I can execute this script without any issues; typing /etc/network/if-down.d/test
into my terminal causes a message box to pop up saying "network down!". When I disconnect from my wifi network via the network manager, nothing happens. Unplugging my wifi usb dongle does not cause the message box to appear either. My only guess is that for what ever reason scripts in /etc/network/if-down.d/
are not being executed. Adding #!/bin/bash
as the first line didn't work either.
EDIT: 2013-01-02
I had some issues using gertvdijk's answer (old edits and comments getting mixed up) which are now sorted out. Running zenity
with su
and DISPLAY=:0
solved my problem.
Solution 1:
If you're using Network Manager (opposed to the command-line /etc/network/interfaces
file), you should use the Network Manager dispatcher scripts instead.
Simply place your script in the /etc/NetworkManager/dispatcher.d/
directory, similar to the if-down.d
approach. Scripting with Network Manager dispatcher scripts is rather easy and I suggest to read this (basic) example based as posted on the Arch Wiki:
#!/bin/sh
INTERFACE=$1 # The interface which is brought up or down
STATUS=$2 # The new state of the interface
USERNAME=gert # ENTER YOUR USERNAME HERE
case "$STATUS" in
'up') # $INTERFACE is up
# you could do something here...
;;
'down') # $INTERFACE is down
# Check for other active interfaces and only act on all down
if [ ! `nm-tool|grep State|cut -f2 -d' '` = "connected" ]; then
/bin/su -l ${USERNAME} -c 'DISPLAY=:0 /usr/bin/zenity --info --text="all network interfaces down"'
fi
;;
esac
For acting on a specific network, see this answer.
And make sure to restart Network Manager to pick up with this new script.
sudo service network-manager restart