Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
"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 3 '13 at 2: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 3 '13 at 2:58
    
Okay. Glad I could help. By the way, next time we could use a chat session here on Stack Exchange. –  gertvdijk Jan 3 '13 at 3:01

3 Answers 3

up vote 2 down vote accepted

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
share|improve this answer
    
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 3 '13 at 1:34
    
@drezabek Sorry I didn't bother testing my own answer first. See my updated answer - it's now working for me. –  gertvdijk Jan 3 '13 at 1: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 3 '13 at 1: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 3 '13 at 1:58
    
1) 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 3 '13 at 2:05

I tried this out (on 10.04) and have the same issue. It looks like the script does not know which display to display the dialog on.

Try replacing your line by:

DISPLAY=0:0 zenity --info --text="network down!"

(If you have several displays, you may need something more subtle.)

share|improve this answer
    
When I added that line, manually running the script gave me an error from zenity and gtk. Check out my question-edit for more details. Thanks for the answer, I really appreciate it! –  drdrez Jan 3 '13 at 1:32
    
The exclamation mark (!) is causing trouble here too. In sh and bash I'm getting the same error bash: !": event not found. Escaping it, e.g. --text="network down\!" fixes this. –  gertvdijk Jan 3 '13 at 1:56
    
@gertvdijk Strange, that only occurs when I run zenity --info --text="network down!" directly from the terminal. That line, alone in a bash script, does not cause any errors, when the bash script is called in the terminal. –  drdrez Jan 3 '13 at 2:22

The other answers are great - probably even better than this one, but, as usual, there's another way to do it - this time without needing system utilities or elevated permissions (sudo).

My system runs kubuntu 12.04, so this code will probably work on your system. It should also work on gnome 2.x as well except that the autostart script goes in a different place.

I had a similar issue where I wanted to know when my computer went offline (because my notebook seems to do this occasionally by disabling wifi by itself and I often didn't notice the little red x in the taskbar.)

What I did was to add a small (executable) script to the ~/.kde/Autostart directory which runs my main script every time KDE starts (this takes care of it having a display and keyboard to use):

#!/bin/bash
if [ -x "$HOME/bin/offline_alert" ] ; then
  offline_alert & 
fi

The main script, offline_alert, tests to see if I'm offline using ping with a few retries to avoid false alarms and then generates a pop-up using yad (same as zenity, only way enhanced!) and plays a sound. In between testing for changes in online status, it sleeps so it doesn't use too many resources.

If you run it manually from the command line, etc., it checks to see if it is already running and gives you the option to end it.

The code that tests for offline is in the "offline" function, so it can easily be modified without affecting the rest of the script.

The images, icons, and sound it uses are hard coded, so you'd have to substitute your own. I found a cool wifi offline icon on the web.

It creates a log file which you may or may not want.

The script is setup to use notifications, but the code is commented out because the notifications went away too quickly and seemed more of a nuisance than a help.

This is just another way to do it and maybe someone will find the code useful.

Joe

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.