I have a pi zero 1.3 running on an ethernet connection (using this hub hat). I am running it headless, so I can only access it via ssh. Every once in a while, the internet connection drops, and I have no choice but to unplug the device and plug it back in. Once it restarts, the connection works as normal.
I know that unplugging the device is bad, so I am trying to come up with a solution that will reestablish the connection automatically, or at least reboot if no connection is detected. I have written the following bash script and set it up as a cron job, but it doesn't seem to be working:
#!/bin/bash #Test for network connection if [[ $(cat /sys/class/net/eth0/carrier) != 1 ]] #if eth0 is not connected then date >> /home/pi/crons/net-check/log.txt #log date echo -e "\nNetwork connection down, restarting eth0..." ifconfig eth0 down && ifconfig eth0 up echo -e "Restart complete!\n" if [[ $(cat /sys/class/net/eth0/carrier) != 1 ]] #check if eth0 is still not connected then echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" >> /home/pi/crons/net-check/log.txt #emphasize log date echo -e "Restart did not fix the issue, rebooting...\n" reboot else echo -e "Connection is up!\n" fi else echo -e "\nConnection is up!\n" fi
I have not yet had the internet cut out on its own since setting this up. I attempted to test it by running
sudo ifconfig eth0 down. As expected this kicks me off ssh, but it looks like the
ifconfig eth0 up is not executing to restart the connection. The log file shows the
^^^^^^ (emphasizing that the connection was not reset), but the
reboot does not execute either. Pinging the IP returns "host unreachable", and the logs look like this afterwards:
The job is running in the root user crontab, so permissions should not be an issue. Here is how the task is set up:
* * * * * /home/pi/crons/net-check/net-check.sh
Any suggestions for why this might not be working? Any advice for logs to check to determine the source of the connection dropping would be helpful as well. Thanks!