3

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.

drdrez
  • 373
  • "So, I have come to conclusion that network events do not run scripts located in /etc/NetworkManager/dispatcher.d/" You're wrong here stating this. It's more likely that you're making a mistake. – gertvdijk Jan 03 '13 at 02:17
  • @gertvdijk I have found the problem; stack exchange doesn't work very well in real time :( I will edit my question again, and mark your answer as the solution. Thank you for all the help! – drdrez Jan 03 '13 at 02:58
  • Okay. Glad I could help. By the way, next time we could use a chat session here on Stack Exchange. – gertvdijk Jan 03 '13 at 03:01

3 Answers3

2

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
gertvdijk
  • 67,947
  • I can't get scripts in /etc/NetworkManager/dispatcher.d/ to get run. Check out my question-edit for more details. Thank you for your answer! I am really grateful! – drdrez Jan 03 '13 at 01:34
  • @drezabek Sorry I didn't bother testing my own answer first. See my updated answer - it's now working for me. – gertvdijk Jan 03 '13 at 01:45
  • Something must not be right... the line /bin/su -l username -c 'DISPLAY=... still causes the gtk/zenity error when I manually execute it, and disconnecting from my wifi network doesn't cause a message box to appear, even when I remove the DISPLAY=0:0 part. I'll update my question to reflect your update. Thanks for the reply! – drdrez Jan 03 '13 at 01:56
  • @drezabek 1) did you replace username with your own username? 2) do not remove the DISPLAY part - it is essential. works for me – gertvdijk Jan 03 '13 at 01:58
  • Yes, I did change the username to my local one. 2) I copied the /bin/su -l username -c 'DISPLAY=... line outside of the loop and changed the text to network event:, and when I ran this manually from my terminal I got the gtk/zentiy I mentioned in my question-edit. Without DISPLAY=0:0, the message box appears without any problems. When I disconnect from my wifi network, I do so with and without the DISPLAY=0:0 portion in the script, just to make sure.
  • – drdrez Jan 03 '13 at 02:05
  • @drezabek You're probably missing out on something too obvious, I guess. It's really working here. I'm on Kubuntu 12.04, though. – gertvdijk Jan 03 '13 at 02:16
  • Yeah, when I have more time, I will look into KDE and networking events. The changes between 12.04 and 12.10 can be pretty drastic, though. I was on Lubuntu 12.04, but in 12.10 they removed/changed the gnome system settings dialog, which I needed to use an application, so I switched to Kubuntu for 12.10. – drdrez Jan 03 '13 at 02:25
  • @drezabek Verified it's working in a clean Quantal Kubuntu installation in a VM. It must be something local with your setup. screenshot. – gertvdijk Jan 03 '13 at 02:36