3

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
4

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"
address 192.168.1.1
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.254
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
-1

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.