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!

  • What is the problem with using the ifplug@.service from the link you have given? – Ingo Dec 18 '20 at 12:45
  • The problem is that I dont know how to write the script or how to make it run automatically and do its stuff. I can follow and modify existing scripts to a point but the examples I have found are too old to work in Buster. The examples I have found are for Wheezy and simply dont translate over to Buster due to changes in networking. – JustSumDad Dec 19 '20 at 17:03
  • Please edit your question to incorporate the additional information you have provided in your comments. Wrt your comment below, please also clarify your reference to "the switch", and timing of power removal. – Seamus Dec 20 '20 at 0:59
  • @Andyroo this is not a smart switch so it has no IP address. The only devices with IP addresses are the PI and several IP cameras that have their own independent power source and may or may not be powered up at any given time. – JustSumDad Dec 20 '20 at 2:51
  • You wrote: "The problem is that I dont know how to write the script or how to make it run automatically and do its stuff." - OK, I understand. I have made a detailed answer. – Ingo Dec 20 '20 at 16:02

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:

Description=Power off the device

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:


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:

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


case "$INTERFACE" in
    case "$EVENT" in
        /usr/bin/systemctl stop pwroff.timer
        /usr/bin/systemctl start pwroff.timer
        >&2 echo empty or undefined event for "$INTERFACE": \""$EVENT"\"
        exit 1

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.

  • THANK YOU!! I will apply and test this tomorrow! – JustSumDad Dec 21 '20 at 6:48
  • Tested and verified works exactly as I wanted thank you so much!! – JustSumDad Dec 21 '20 at 23:40
  • I hate to ask this as you have been so kind, but do you think it would be hard to have the shutdown abort if the link state came back up in the time delay? I set the time delay to 240sec as a test and did an unplug/re-plug test and it still shut down at 4 min... This is something I can live with if its too time consuming. Thanx again! – JustSumDad Dec 22 '20 at 0:58
  • 1
    @JustSumDad Yes,it is possible to abort the shutdown with a systemd timer. I will look at it. But please accept the answer with a click on the tick on its left side. – Ingo Dec 22 '20 at 19:21
  • 1
    @JustSumDad Thanks :-) I have updated the answer. – Ingo Dec 22 '20 at 20:25

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

  • The pi is going to be a headless unattended installation. I need an automated shutdown when power is cut to the switch as there will be a few min between the switch going down and the Pi going down. – JustSumDad Dec 18 '20 at 6:08
  • 3
    The question is: "How to make Raspian Buster shutdown when I unplug the ethernet cable?" This cannot be done by pushing a button. – Ingo Dec 19 '20 at 18:05
  • @JustSumDad: Could you please edit your question to explain what is meant by your comment: "` I need an automated shutdown when power is cut to the switch as there will be a few min between the switch going down and the Pi going down.`" Specifically, we need to understand the relationship of "the switch" to the pi... is this an Ethernet switch, a power switch - or something else entirely? – Seamus Dec 20 '20 at 0:19
  • @Seamus I am sorry I thought the title of the thread would have indicated I am looking for a network related solution. Ethernet switch. – JustSumDad Dec 20 '20 at 2:39
  • 1
    I probably contributed to the confusion. I read the OP's ORIGINAL question and later wrote a reply. Unfortunately by the time I replied I posted the Answer to the duplicate (which omitted much of the details) which was an XY question. – Milliways Dec 20 '20 at 3:00

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.

  • The main thing I am looking for in a shutdown command is to close off all open files to prevent corruption when the power is removed. The RPI is functioning as an NVR for several cameras and the video files need to be closed in an orderly fashion. "shutdown now" does this. As the PI will be in a headless unattended system I am working on a way to effect automatic shutdown before power is removed. This is the main goal. Ingo has achieved this with the script they supplied. – JustSumDad Dec 22 '20 at 6:53
  • I am now hoping for an additional change to allow the system to abort the shutdown timer IF the network switch comes back online before the "shutdown now" is executed. – JustSumDad Dec 22 '20 at 6:53

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.