4

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.

Flipacholas
  • 143
  • 5

3 Answers3

6

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.

Bex
  • 2,929
  • 3
  • 25
  • 34
gurcanozturk
  • 3,748
  • 1
  • 19
  • 17
  • 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 00:45
0

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.

#!/bin/sh

/sbin/shutdown -h now
mrm
  • 103
  • 5
-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.

jet
  • 379
  • 2
  • 9