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
.
ifplug@.service
from the link you have given? – Ingo Dec 18 '20 at 12:45the switch
", and timing of power removal. – Seamus Dec 20 '20 at 00:59