1

I have been looking at old information and messing around a lot, but I ended up lost :(

I need my RPI4 running Buster to execute a shutdown script when the ethernet port state changes from plugged in/active to unplugged/inactive. Hopefully with a set delay. I realize I could instead use ping to monitor the response of some other hardware on the network however there is no single piece of hardware that can be counted on to be active, they are all IP cameras that may or may not be available at any given time. This is why I need it to be based on the ethernet port state. I also need this script to check for the eth0 state a few times before shutting down just in case there is a momentary state change.

I have followed this: Make ifplugd available again since Raspbian Wheezy without using ifupdown

But I then realized most if not all of the information from old posts did not apply with ifplugd so I had no script example to follow.

The nearest useful script example I could find was six years old :( How to make Raspberry shutdown when I unplug the ethernet cable

It is my best guess that what I want is a cron job that checks the state via the ifplugd command that I have now in theory made work, then hand it to the shutdown script I have made and tested. I simply do not know how to implement this.

I hope I have made this clear enough now.

Thanx much!

JustSumDad
  • 29
  • 5

3 Answers3

3

You can use ifplugd according to the link you have given: Make ifplugd available with systemd. Just follow section Install ifplugd program and section Create new service for the ifplugd program. Because you want to abort the shutdown delay if the interface comes up within its time, we need a systemd.timer, that can be started and stopped. First create the timer with its corresponding service. Details about it is out of scope here. Please look at the documentation. Create the service. Don't use name poweroff because it may conflict with existing services.

rpi ~$ sudo -Es   # if not already done
rpi ~# systemctl edit --force --full pwroff.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=Power off the device

[Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/sbin/halt --poweroff --no-wall

Create the corresponding timer. It must have the same name pwroff but with extension timer:

rpi ~# systemctl edit --force --full pwroff.timer

In the empty editor insert these statements, save them and quit the editor:

[Timer]
OnActiveSec=30

This will activate the pwroff.service after 30 seconds and poweroff the device, when you start the timer. Manage it with:

rpi ~# systemctl start pwroff.timer
rpi ~# systemctl stop pwroff.timer
rpi ~# systemctl status pwroff.timer
rpi ~# systemctl edit --full pwroff.timer

Then create this new Action Script for the ifplug service:

rpi ~# mkdir /etc/ifplugs
rpi ~# editor /etc/ifplugs/ifplugs.action

In the empty editor insert these statements, save them and quit the editor:

#!/usr/bin/bash
# redirect all output into a logfile for debug
#exec 1>> /tmp/ifplugs-debug.log 2>&1

INTERFACE="$1" EVENT="$2"

case "$INTERFACE" in eth0) case "$EVENT" in up) /usr/bin/systemctl stop pwroff.timer ;; down) /usr/bin/systemctl start pwroff.timer ;; *) >&2 echo empty or undefined event for "$INTERFACE": ""$EVENT"" exit 1 ;; esac ;; esac

Make the script executable:

rpi ~# chmod +x /etc/ifplugs/ifplugs.action
rpi ~# exit

Then you can test it with:

rpi ~$ sudo /etc/ifplugs/ifplugs.action eth0 down
rpi ~$ sudo /etc/ifplugs/ifplugs.action eth0 up

Enable the service with:

rpi ~$ sudo systemctl enable --now ifplug@eth0.service

You can monitor what it is doing with:

rpi ~$ journalctl --unit=ifplug@eth0.service
-- Logs begin at Thu 2019-02-14 10:11:59 GMT, end at Sun 2020-12-20 15:02:50 GMT. --
Dec 20 14:15:35 raspberrypi systemd[1]: Started Interface plug monitor (interface-specific version).
Dec 20 14:15:35 raspberrypi ifplugd[515]: ifplugd 0.28 initializing.
Dec 20 14:15:35 raspberrypi ifplugd[515]: Using interface eth0/DC:A6:32:7F:38:46 with driver <bcmgenet> (version: v2.0)
Dec 20 14:15:35 raspberrypi ifplugd[515]: Using detection mode: SIOCETHTOOL
Dec 20 14:15:35 raspberrypi ifplugd[515]: Initialization complete, link beat not detected.
Dec 20 15:02:17 raspberrypi ifplugd[515]: Link beat detected.
Dec 20 15:02:17 raspberrypi ifplugd[515]: Executing '/etc/ifplugs/ifplugs.action eth0 up'.
Dec 20 15:02:17 raspberrypi ifplugd[515]: Program executed successfully.

If you unplug the ethernet cable, the RasPi will graceful poweroff after the given time in pwroff.timer.

Ingo
  • 42,961
  • 20
  • 87
  • 207
1

Rather than trying to develop some esoteric script you can safely shutdown the Pi with a button (or relay contact) with dtoverlay=gpio-shutdown.

NO code required!

See https://raspberrypi.stackexchange.com/a/77918/8697

Milliways
  • 62,573
  • 32
  • 113
  • 225
1

After your last revision, I don't really know what you want at all. I'm posting this only because I've invested time in it.


I think that part of the answer to your question is covered in this Q&A on Unix&Linux SE site. In other words, you can read the state of your network device eth0, and take action on the basis of that. If I were doing this for myself, I would at least consider writing a small bash script for the implementation; systemd may offer more elegant solutions, but I'm not conversant in systemd.

Your (pre-revision) wish to "check for the eth0 state a few times before shutting down" could also be accommodated in this script. However, as your question is unclear - at least to me, I'll postpone proposing a script until the question becomes clear.

A couple of things to keep in mind:

  1. We should be clear on what "shutdown" means for the RPi; "shutdown" is a rather generic term, but shutdown is a command that has a specific - but machine-dependent - meaning. On most computers, invoking shutdown causes the machine to gracefully stop processing, and remove power - this is what happens when (for example) I issue the command sudo shutdown on my Ubuntu laptop machine. However, the Raspberry Pi responds differently to the shutdown command - power is not removed, but processing is stopped so that it is safe to remove powerREF.

  2. It will be helpful to have a monitor attached to your RPi for testing your scripts as your SSH connection will be lost when your local network services are disconnected/turned off.

Good luck.

Seamus
  • 23,558
  • 5
  • 42
  • 83