Dietpi -> wired reconnect script not working

Hi Guy’s,

I have a little problem with Dietpi.
Every time I restart my router, my Raspberry logically loses the connection and then remains unreachable for the router.

The setup is wired and i don’t use Wifi.
Now I want to use a small script which should help the Pi with the suddenly missing connectivity, the interface either restarts or the Pi reboots itself.
Unfortunately, something does not work here:

The Pi simply does not reboot or cannot reconnect.
Does anyone have a tip or maybe can help ?

nano /usr/local/bin/ethernet_reloader.sh
#!/bin/bash

wget -q --spider https://duckduckgo.com/

# If the return code from wget ($?) is not 0 (meaning there was an error)
if [ $? != 0 ]
then
    # Restart the Pi
    sudo reboot
fi
chmod +x /usr/local/bin/ethernet_reloader.sh
crontab -e
*/5 * * * * sh /usr/local/bin/ethernet_reloader.sh >/dev/null 2>&1

Alternative with restarting eth0

#!/bin/bash

wget -q --spider https://duckduckgo.com/

# If the return code from ping / wget ($?) is not 0 (meaning there was an error)
if [ $? != 0 ]
then
    # Restart the wireless interface
    sudo ifdown --force eth0
    sudo ifup eth0
    # Sleep for 45 seconds to give a chance to the network adapter to get an IP
    sleep 45s
fi

How does it behave if your remove the Ethernet cable manually and plug it back again. Does your RPi get’s reconnected?

I did a test on my RPi4B and it automatically reconnect once I connect Ethernet again

dmesg

[79653.870336] bcmgenet fd580000.ethernet eth0: Link is Down
[79675.374391] bcmgenet fd580000.ethernet eth0: Link is Up - 1Gbps/Full - flow control rx/tx

journalctl

Aug 28 13:13:16 DietPi4 kernel: bcmgenet fd580000.ethernet eth0: Link is Down
Aug 28 13:13:37 DietPi4 kernel: bcmgenet fd580000.ethernet eth0: Link is Up - 1Gbps/Full - flow control rx/tx

Do you have access to your RPi (monitor, keyboard attached) to check if the Ethernet connection is recognizing ?

rechecked:

I run my Pi, headless.
Removing the Ethernet cable manually and plug it back again.
Leads to the fact that the Pi no longer connects to the network, a reboot helps here.

:confused:

Are you able to connect a screen to check if the connection is recognised at all?

Or do you have a Hub/Switch flying around, that could be used for testing. Would be interesting to see how the RPi behaves if it is connected to a different network component than the Router.

Not really :confused:

i think i messed something up with cron or permissions.

hmm quite hard to check if the Ethernet connection is recognized at all without screen attached.

What you could try to trigger a reboot, is to have a look into the tool ifplugd. We played a little bit in the past to switch Ethernet to WiFi once Ethernet got detached. But it should work in your case as well. Once the network cable removal is detected, a reboot could be triggered. It’s just a matter how you define the ifplugd.action

https://dietpi.com/forum/t/using-ethernet-wlan-interfaces-not-at-the-same-time-in-v6-30/4251/1

The alternative script should work, skipping the complete reboot. To debug, could you add the following lines before “ifdown” to check which layers of the connection/interface got lost:

ip l
ip a
ip r

Btw, the sleep has no effect. Post-processing steps of ifup, like WPA connection, DHCP lease and such do run as own daemons independent of the parent shell. So when ifup stops blocking, the script can exit without issues. And generally, I’d always recommend a static IP for a server. Usually you can give it a “reserved” IP in the router/DHCP server, so that this IP won’t be given to any other device, then you can make it a static IP on the server, so that it can skip the time consuming DHCP lease.