Dietpi -> wired reconnect script not working

Having issues with your DietPi installation or found a bug? Post it here.
Post Reply
TBirth
Posts: 8
Joined: Sat Aug 28, 2021 12:39 pm

Dietpi -> wired reconnect script not working

Post by TBirth »

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 ?

1.

Code: Select all

nano /usr/local/bin/ethernet_reloader.sh
2.

Code: Select all

#!/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
3.

Code: Select all

chmod +x /usr/local/bin/ethernet_reloader.sh
4.

Code: Select all

crontab -e
5.

Code: Select all

*/5 * * * * sh /usr/local/bin/ethernet_reloader.sh >/dev/null 2>&1

Alternative with restarting eth0

Code: Select all

#!/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
Last edited by TBirth on Sat Aug 28, 2021 2:44 pm, edited 1 time in total.
User avatar
Joulinar
Posts: 5619
Joined: Sat Nov 16, 2019 12:49 am

Re: Dietpi -> wired reconnect script not working

Post by Joulinar »

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

Code: Select all

[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

Code: Select all

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 ?
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
TBirth
Posts: 8
Joined: Sat Aug 28, 2021 12:39 pm

Re: Dietpi -> wired reconnect script not working

Post by TBirth »

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.

:/
User avatar
Joulinar
Posts: 5619
Joined: Sat Nov 16, 2019 12:49 am

Re: Dietpi -> wired reconnect script not working

Post by Joulinar »

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.
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
TBirth
Posts: 8
Joined: Sat Aug 28, 2021 12:39 pm

Re: Dietpi -> wired reconnect script not working

Post by TBirth »

Not really :/

i think i messed something up with cron or permissions.
User avatar
Joulinar
Posts: 5619
Joined: Sat Nov 16, 2019 12:49 am

Re: Dietpi -> wired reconnect script not working

Post by Joulinar »

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

viewtopic.php?f=15&t=7835
Pls let us know if a solution is working. This could help others if they hit by similar situation. Your DietPi Team
User avatar
MichaIng
Site Admin
Posts: 3216
Joined: Sat Nov 18, 2017 6:21 pm

Re: Dietpi -> wired reconnect script not working

Post by MichaIng »

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:

Code: Select all

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.
Post Reply