0

I have a Raspberry Pi4 24h connected to the internet via Wifi. Sometimes, I think due some signal loss, it don't reconnect until I reboot it.

I've already tried to run a script that automatically monitor the wifi connection and run sudo systemctl restart networking.service but it didn't solve my issue.

So, is there any benefit if I install the NetworkManager on Rpi? Can it reconnect to wifi networks or another feature that can help?

I'm running Raspbian Buster.

  • @Dougie How on earth is an answer based on Wheezy a duplicate of a Question on a (presumably) current OS? – Milliways Jun 26 '20 at 11:21
  • @Milliways because the basic principle is identical. Ping something, restart networking when the ping fails. – Dougie Jun 26 '20 at 11:41
  • Please note l didn't vote to close as duplicate (because it isn't) but because you haven't supplied any details – Milliways Jun 26 '20 at 22:39
  • This is NOT duplicated. If you read this another post you will see that it's talking about how to restart wifi when *using a dongle adapter". I want to know how to restart the default system, without an external adaptor. – Vinícius Valente Jun 27 '20 at 13:22

2 Answers2

1

I have several Pi's that run 24/7 and they will automatically reconnect when the wifi is available (I deliberately have my wifi networks turn off at night when there is no need for them to be on). I have found the below to be 100% reliable. I am using static IP on my Pi's but it should work fine with DHCP as well:

sudo nano /usr/local/bin/checkwifi.sh

Enter this code to restart network (change 192.168.1.1 for an IP address that you expect to always be available, typically your internet router):

ping -c4 192.168.1.1 > /dev/null
 
if [ $? != 0 ] 
then
  echo "No network connection, restarting wlan0"
  /sbin/ifdown 'wlan0'
  sleep 5
  /sbin/ifup --force 'wlan0'
fi

You need schedule this to run regularly. I chose every five minutes:

crontab -e 

Add:

*/5 * * * * /usr/bin/sudo -H /usr/local/bin/checkwifi.sh >> /dev/null 2>&1
PeteC
  • 341
  • 1
  • 3
  • I thought ifup / if down where being depreciated in favour of iwconfig and ifconfig as the ifup/down only read the etc/network/interfaces file as they are part of the Network Manager software stack? Note - this is based on my memory and that can be iffy on a good day :-) –  Jun 26 '20 at 09:57
  • I have no idea if they are being deprecated - I set it up over three years ago and it's been good since then. It may be that some tweaks need to be made for the current build of Raspbian. While I have started from scratch since then and not had an issue, I tend not to keep the base code up to date (they work so if it ain't broke, I don't want to break it with an update!) – PeteC Jun 26 '20 at 10:14
  • This will do **NOTHING** on a properly configured *Raspberry Pi OS* using default networking. – Milliways Jun 26 '20 at 11:25
  • Yeah, @Andyroo and Petec are right... I've tried here and both ifdown and ifup does nothing. They are depreciated. See [this thread](https://www.raspberrypi.org/forums/viewtopic.php?t=213550) for more information... – Vinícius Valente Sep 19 '20 at 21:56
0

The default dhcpcd is reliable and will automatically connect if correctly configured. See How to set up networking/WiFi

Raspberry Pi OS DOES NOT use networking.service - unless you misconfigure it.

You can use NetworkManager, but it is difficult to configure manually, and the GUI networking tool in Raspbian won't work. It is no more reliable than dhcpcd. In addition it needs to be re-configured for each Pi - you can't simply swap SD Cards.

Milliways
  • 54,718
  • 26
  • 92
  • 182
  • I can see on the link you provide the following: "Restart network via sudo service networking restart". Isn't the same as "restart networking.service"? – Vinícius Valente Jun 26 '20 at 03:40
  • @ViníciusValente If you read the full post you will see I DO NOT recommend static IP, and for those that do to use dhcpcd. – Milliways Jun 26 '20 at 03:57
  • I'm not using Static IP. I have internet connection with RPi default configuration. I'm just looking for a better way to reset if it gets down. According with this link, maybe a "sudo systemctl stop dhcpcd && sudo systemctl start dhcpcd" can help to "refresh" the device connection? – Vinícius Valente Jun 26 '20 at 04:26
  • Instead of using two commands you can use `sudo systemctl restart dhcpcd.service`. – Ingo Jun 26 '20 at 10:35