How can I run a command automatically and immediately when my wired or wireless connection disconnects on debian 8.3 stable (Gnome)?

1 Answer 1


On Debian and derivatives, scripts under /etc/network/if*.d are executed when an interface goes up or down. This is a feature of Debian's static network configuration mechanism through /etc/network/interfaces which NetworkManager also executes. So add a script in /etc/network/if-down.d or /etc/network/if-post-down.d depending on whether you want your script to be executed before or after the interface goes down. In each directory, the scripts are executed in lexicographic order, so you may want to start your script's name with 0 or zzzz if you need it to execute first or last. Note that the name of the script is subject to constraints: stick to the characters A-Za-z0-9-_ (in particular no .).

If you need information about the network connection that's going down, you can use the environment variables listed in the interfaces(5) man page. If you need information about the remaining network connectivity, you can call ifconfig, route, ip, etc.

  • Thank you for your answer. I decided to run a script when network connects. So I put my script in /etc/network/if-up.d/ folder. Its name is "0-connect_to_vpn.sh". It is executable and I can run it on terminal without error. However this script does not run automatically when my ethernet connection establishes. It never runs. What can be the reason?
    – harmancode
    Mar 18, 2016 at 13:38
  • "/etc/network/interfaces" configuration file does not contain any entry about ethernet connection. If I add "auto eth0" and in next line "iface eth0 inet dhcp", it stops working after reboot, with the message "Unmanaged" on Network Manager (Gnome). Can this be the reason of why my script does not run?
    – harmancode
    Mar 18, 2016 at 14:48
  • 1
    @dx486 Don't add an entry to /etc/network/interfaces: if you do then NetworkManager stops managing the connection. I only mentioned that because that's where the if-up/down script mechanism is documented. There are naming constraints on the script that I never remember by heart, try again with a script called 0-connect_to_vpn (no dot). Mar 18, 2016 at 16:03
  • A warning: Putting a script in such folders may cause system boot failure and even debian recovery mode becomes inaccessible. System hangs forever on the message: "[ *** ] A start job is running for LSB: Raise network interfaces." with the 3 asterisks moving back and forth. Be careful.
    – harmancode
    Mar 20, 2016 at 15:01
  • @dx486 Well, yes, that script runs as root and runs during the boot process, so it does have the potential to cause trouble. Make sure that whatever's in there doesn't lock up. Mar 20, 2016 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .