Wednesday, May 1, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 1836  / 2 Years ago, sat, november 27, 2021, 9:49:49

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.


More From » networking

 Answers
3

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

[#33407] Sunday, November 28, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
uffno

Total Points: 283
Total Questions: 93
Total Answers: 111

Location: Saint Vincent and the Grenadines
Member since Thu, Oct 15, 2020
4 Years ago
uffno questions
Thu, Sep 2, 21, 18:08, 3 Years ago
Sun, Apr 2, 23, 15:15, 1 Year ago
Sat, Jan 8, 22, 16:37, 2 Years ago
;