I use my Raspberry Pi Model B with Raspbian as a server and I want to know (if it is possible) how to make that the Raspberry pi send itself a halt command when I unplug the Ethernet cable (eth0)

Any help will be very appreciated.

  • Why? If you unplug it won't work! Why halt - to save 5w? The solutions below would work, but would also halt on router glitches. – Milliways Aug 18 '14 at 12:41
  • True, so is there any way to detect if it's "physically" unplugged from eth0? – Flipacholas Aug 18 '14 at 19:51

post-down script;

Write a script to call 'reboot' as /path/to/script.sh

Edit your /etc/network/interfaces file and add a line just below your eth0 config.

auto eth0
iface eth0 inet static
name "Ethernet0"
post-down /path/to/script.sh

Your /path/to/script.sh script will run when your eth0 down.

  • Don't encourage people to periodically poll for events! Your second option is a lot better, you say for yourself - so why don't remove your first option? – Bex Aug 18 '14 at 12:52
  • @Bex you're right. – gurcanozturk Aug 18 '14 at 14:02
  • This is valid for older Raspbian versions, like wheezy. For buster, there's a directory of scripts that you can just drop your shutdown script into. I edited this answer to add this, but someone rejected the edit (!?). See my answer if you're on buster. – mrm Jun 29 '19 at 0:45

For recent Raspbian builds (like Buster)

Just drop your shutdown script into the /etc/network/if-post-down.d directory. Remember to make the script executable, as well.


/sbin/shutdown -h now
  • Just on recent versions of Raspbian/Debian you should avoid using deprecated ifupdown networking. Instead systemd unit files should be used. – Ingo Jun 29 '19 at 13:11
  • Thanks for the hint, @ingo. Do you have a pointer to a working examples? The first couple pages of search results were people asking for systemd help and failing. – mrm Jun 30 '19 at 1:12

Create a cron job every minute to ping your gateway, the moment the ping is lost- issue halt command. You can check for example for 2 consecutive pings to be sure is not a glitch.

  • when your router reboot or down also your RPi will reboot? – gurcanozturk Aug 18 '14 at 12:34
  • 2
    You should never periodically poll for an event! – Bex Aug 18 '14 at 12:51
  • can you explain why not polling – jet Aug 18 '14 at 15:20
  • @jet Because 1) it consumes resources unnecessarily, and 2) it is inherently slow. – Bex Aug 18 '14 at 19:26
  • if you don't poll - the kernel's scheduler is doing it for you (every 100mS, I am doing it once per minute). The only nonpolling events are interruptions, everything else is polling somewhere. – jet Aug 18 '14 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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