-
- Posts: 1
- Joined: Thu Aug 30, 2012 7:07 pm
Wifi Reconnect on drop
Hi,
Quite please with myself. Recently go my Pi and this evening managed to get the Edimax ew-7811un working with it (after having issues removing another adaptor I had tried).
Anyway the only slight issue I have is if the wifi drops (e.g. router reboots) then it doesn't auto reconnect. I have to unplug the adaptor and plug it back in again. As I normally connect by SSH rather than keyboard/monitor I can't just restart the networking service. Obviously I can't go unplugging/reconnecting the adaptor if I'm not at home when it drops.
Any ideas on how to force it to keep retrying the access point if it disappears for a few seconds?
Thanks!
Quite please with myself. Recently go my Pi and this evening managed to get the Edimax ew-7811un working with it (after having issues removing another adaptor I had tried).
Anyway the only slight issue I have is if the wifi drops (e.g. router reboots) then it doesn't auto reconnect. I have to unplug the adaptor and plug it back in again. As I normally connect by SSH rather than keyboard/monitor I can't just restart the networking service. Obviously I can't go unplugging/reconnecting the adaptor if I'm not at home when it drops.
Any ideas on how to force it to keep retrying the access point if it disappears for a few seconds?
Thanks!
Re: Wifi Reconnect on drop
I think the easiest way is installing a network manager like wicd
Re: Wifi Reconnect on drop
You could try this script.colin79666 wrote:Hi,
Quite please with myself. Recently go my Pi and this evening managed to get the Edimax ew-7811un working with it (after having issues removing another adaptor I had tried).
Anyway the only slight issue I have is if the wifi drops (e.g. router reboots) then it doesn't auto reconnect. I have to unplug the adaptor and plug it back in again. As I normally connect by SSH rather than keyboard/monitor I can't just restart the networking service. Obviously I can't go unplugging/reconnecting the adaptor if I'm not at home when it drops.
Any ideas on how to force it to keep retrying the access point if it disappears for a few seconds?
Thanks!
Code: Select all
#!/bin/bash
while true ; do
if ifconfig wlan0 | grep -q "inet addr:" ; then
sleep 60
else
echo "Network connection down! Attempting reconnection."
ifup --force wlan0
sleep 10
fi
done
Code: Select all
sudo chmod +x ./network-monitor.sh
Code: Select all
sudo ./network-monitor.sh &
Code: Select all
fg
Tested it here in a couple of ways. First powering off my wifi access point. The script detects no network connection and starts attempting to force a reconnect. Powered the access point back on and after a couple of minutes the connection is re-established. For another test I removed the MAC address from the list of allowable addresses in the access point MAC filter. The network connection went down. Re-enabled the MAC address and it came back up again after a minute or two.
MrEngman
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra
Please post ALL technical questions on the forum. Please Do Not send private messages.
Please post ALL technical questions on the forum. Please Do Not send private messages.
Re: Wifi Reconnect on drop
Or instead of using 'sleep' and running in the background, you could set up a cron
Code: Select all
sudo crontab -e
Re: Wifi Reconnect on drop
Looks overly complicated. If I want to check every minute I've got to enter 60 commands in to crontab, and then if it takes longer than a minute to reconnect, which is very likely, then cron will start additional commands. So could end up with multiple copies of the command running and I'd hate to think what might happen then.mattura wrote:Or instead of using 'sleep' and running in the background, you could set up a cronCode: Select all
sudo crontab -e
So modify the script to check if it is already running and abort if it is. Hmm. Guess the best method is whichever takes least computing time. Think I'll stick with my script.
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra
Please post ALL technical questions on the forum. Please Do Not send private messages.
Please post ALL technical questions on the forum. Please Do Not send private messages.
Re: Wifi Reconnect on drop
@MrEngman: Why would you have to enter 60 commands? Wouldn't prefixing it with "* * * * *" make it run every minute from crontab?
Anyway, thanks for the script, I'm going to try it out!
Anyway, thanks for the script, I'm going to try it out!
Re: Wifi Reconnect on drop
OK maybe got that wrong then. But the script will still need updating to ensure only one copy is running.loneboat wrote:@MrEngman: Why would you have to enter 60 commands? Wouldn't prefixing it with "* * * * *" make it run every minute from crontab?
Anyway, thanks for the script, I'm going to try it out!
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra
Please post ALL technical questions on the forum. Please Do Not send private messages.
Please post ALL technical questions on the forum. Please Do Not send private messages.
Re: Wifi Reconnect on drop
Without any 'sleep' commands it is highly unlikely that the script will take as long as 60 seconds to finish executing! Just remove the 'sleep's and the while loop, and put it in the cron list. Then every minute the cron will cause the script to execute, the script will check the network connection and restart it if it is down, then the script will finish. A minute later, the cron will cause the script to execute again, and so on...MrEngman wrote:OK maybe got that wrong then. But the script will still need updating to ensure only one copy is running.loneboat wrote:@MrEngman: Why would you have to enter 60 commands? Wouldn't prefixing it with "* * * * *" make it run every minute from crontab?
Anyway, thanks for the script, I'm going to try it out!
Re: Wifi Reconnect on drop
Even if the script is modified so it doesn't loop but just executes once without looping it can take some time if the router is down. Now if it executed every 2 minutes I would be happier with that, so how can crontab do that?mattura wrote:Without any 'sleep' commands it is highly unlikely that the script will take as long as 60 seconds to finish executing! Just remove the 'sleep's and the while loop, and put it in the cron list. Then every minute the cron will cause the script to execute, the script will check the network connection and restart it if it is down, then the script will finish. A minute later, the cron will cause the script to execute again, and so on...MrEngman wrote:OK maybe got that wrong then. But the script will still need updating to ensure only one copy is running.loneboat wrote:@MrEngman: Why would you have to enter 60 commands? Wouldn't prefixing it with "* * * * *" make it run every minute from crontab?
Anyway, thanks for the script, I'm going to try it out!
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra
Please post ALL technical questions on the forum. Please Do Not send private messages.
Please post ALL technical questions on the forum. Please Do Not send private messages.
Re: Wifi Reconnect on drop
Script uses/checks for a lockfile, allows you to set what interface. Run from cron, I would do it every 5 minutes, but every 2 or whatever will work (example cron entry in code)
https://raw.github.com/dweeber/WiFi_Che ... WiFi_Check
https://raw.github.com/dweeber/WiFi_Che ... WiFi_Check
Dweeber A.K.A. Kevin...
My RPI Info Pages including Current Setup - http://rpi.tnet.com
My RPI Info Pages including Current Setup - http://rpi.tnet.com
Re: Wifi Reconnect on drop
Sorry to drag up an old thread, but this is exactly the sort of script I'm looking for. I've followed MrEngman's instructions and it works beautifully, however how do I now get this script (placed in /home as instructed!) to auto-execute in case the Pi reboots itself?MrEngman wrote:You could try this script.colin79666 wrote:Hi,
Quite please with myself. Recently go my Pi and this evening managed to get the Edimax ew-7811un working with it (after having issues removing another adaptor I had tried).
Anyway the only slight issue I have is if the wifi drops (e.g. router reboots) then it doesn't auto reconnect. I have to unplug the adaptor and plug it back in again. As I normally connect by SSH rather than keyboard/monitor I can't just restart the networking service. Obviously I can't go unplugging/reconnecting the adaptor if I'm not at home when it drops.
Any ideas on how to force it to keep retrying the access point if it disappears for a few seconds?
Thanks!Copy the code to a file network-monitor.sh in your home directory. Then run the commandCode: Select all
#!/bin/bash while true ; do if ifconfig wlan0 | grep -q "inet addr:" ; then sleep 60 else echo "Network connection down! Attempting reconnection." ifup --force wlan0 sleep 10 fi done
to set it as executable. Run it in the background using the commandCode: Select all
sudo chmod +x ./network-monitor.sh
It checks every 60 seconds if your wifi has a network connection. If it finds it has no network address it will attempt to force a reconnect and continue doing that until a connection is re-established. If you want to stop it as it is running in the background first use the commandCode: Select all
sudo ./network-monitor.sh &
This will force it to the foreground and you can then stop it using cntl-c.Code: Select all
fg
Tested it here in a couple of ways. First powering off my wifi access point. The script detects no network connection and starts attempting to force a reconnect. Powered the access point back on and after a couple of minutes the connection is re-established. For another test I removed the MAC address from the list of allowable addresses in the access point MAC filter. The network connection went down. Re-enabled the MAC address and it came back up again after a minute or two.
MrEngman
Re: Wifi Reconnect on drop
Easiest would be to place it in /etc/rc.local
Before the exit 0 at the end of that file add:
Assuming it is located in /home... If not change the path to where it is located.
Before the exit 0 at the end of that file add:
Code: Select all
/home/network-monitor.sh &
Dweeber A.K.A. Kevin...
My RPI Info Pages including Current Setup - http://rpi.tnet.com
My RPI Info Pages including Current Setup - http://rpi.tnet.com
Re: Wifi Reconnect on drop
This code works for me; thanks!
Does anyone understand why the wifi connection drops and whether anyone is working on a fix?
Does anyone understand why the wifi connection drops and whether anyone is working on a fix?
Re: Wifi Reconnect on drop
Lots of reasons for the connection to drop...rmcd wrote:This code works for me; thanks!
Does anyone understand why the wifi connection drops and whether anyone is working on a fix?
o poor signal
o problem with access point
o interference from another access point
o problem with RPi
o Something in the software you are using
o Problems with power
...
Mine typically stay online for days (weeks) at a time.
Note that other computer platforms typically have processes that will reestablish a connection if it is lost or seek out another authorized connection if one is present. So you may not notice a flaky Access Point on those types of devices.
The script is essentially doing the same thing on the RPi....
Dweeber A.K.A. Kevin...
My RPI Info Pages including Current Setup - http://rpi.tnet.com
My RPI Info Pages including Current Setup - http://rpi.tnet.com
Re: Wifi Reconnect on drop
Thanks @Dweeber. I understand there can be lots of reasons, but there are two strange aspects:
1. from googling, there are a lot of people who think that previous wheezy images did not have this problem, but that it started after updates. This is anecdotal, of course, and I can't judge
2. my ubuntu laptop runs network-manager and automatically reconnects if there is a drop (which almost never happens for the access point I'm using with the pi). I don't understand why the pi doesn't do this. Syslog shows an error message about possible multiple wpa_supplicant processes:
1. from googling, there are a lot of people who think that previous wheezy images did not have this problem, but that it started after updates. This is anecdotal, of course, and I can't judge
2. my ubuntu laptop runs network-manager and automatically reconnects if there is a drop (which almost never happens for the access point I'm using with the pi). I don't understand why the pi doesn't do this. Syslog shows an error message about possible multiple wpa_supplicant processes:
Code: Select all
Dec 15 10:35:33 raspberrypi wpa_supplicant[4034]: Failed to initialize control interface 'DIR=/var/run/wpa_supplicant GROUP=netdev'.#012You may have another wpa_supplicant process already running or the file was#012left by an unclean termination of wpa_supplicant in which case you will need#012to manually remove this file before starting wpa_supplicant again.
Re: Wifi Reconnect on drop
I have the same problem, i logged in a root, but i don;t really know where the home folder is - i just saved the file to the top level folder and did everything else
does it really matter where the script is placed?
thanks Ergman for this script
does it really matter where the script is placed?
thanks Ergman for this script
Re: Wifi Reconnect on drop
oooops, one more question will the script start up everytime my Pi starts???
Also i have the network-monitor file stored in the same folder with
.bash-history
.bash-irc
.profile
is this ok???
Also i have the network-monitor file stored in the same folder with
.bash-history
.bash-irc
.profile
is this ok???
Re: Wifi Reconnect on drop
I'm not sure which script you're talking about. Are you running the shell script that loops (this is what I'm guessing), or are you using crontab?
If you're running the shell script that loops, it will not run on startup unless you explicitly make that happen. You could for example edit /etc/rc.local and put a reference to the script (with a full path) there. Anything run from rc.local will execute when the OS boots.
If you're using cron, the cron service runs automatically each time the pi starts, so there's no need for you to do anything after you've set it up.
You can find out where you've stored the script by running the command pwd (print working directory). The location is probably /home/pi .
I run the script every 5 minutes using cron, which is what I would recommend. Do the following:
1. sudo crontab -e
2. At the bottom of the file, enter a line that looks like this (this presumes your script is named "network-monitor.sh"
# m h dom mon dow command
*/5 * * * * /home/pi/network-monitor.sh
3. save the file and you're done! The script will run every 5 minutes. If you omit "/5", it will run every minute.
If you're running the shell script that loops, it will not run on startup unless you explicitly make that happen. You could for example edit /etc/rc.local and put a reference to the script (with a full path) there. Anything run from rc.local will execute when the OS boots.
If you're using cron, the cron service runs automatically each time the pi starts, so there's no need for you to do anything after you've set it up.
You can find out where you've stored the script by running the command pwd (print working directory). The location is probably /home/pi .
I run the script every 5 minutes using cron, which is what I would recommend. Do the following:
1. sudo crontab -e
2. At the bottom of the file, enter a line that looks like this (this presumes your script is named "network-monitor.sh"
# m h dom mon dow command
*/5 * * * * /home/pi/network-monitor.sh
3. save the file and you're done! The script will run every 5 minutes. If you omit "/5", it will run every minute.
Re: Wifi Reconnect on drop
Root's home folder is /rootbazpaul wrote:I have the same problem, i logged in a root, but i don;t really know where the home folder is - i just saved the file to the top level folder and did everything else
does it really matter where the script is placed?
thanks Ergman for this script
Everybody else's is in /home/username
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......
I must not tread on too many sacred cows......
-
- Posts: 1
- Joined: Thu Feb 14, 2013 9:45 pm
Re: Wifi Reconnect on drop
Code: Select all
#!/bin/bash
while true ; do
if ifconfig wlan0 | grep -q "inet addr:" ; then
sleep 60
else
echo "Network connection down! Attempting reconnection."
ifup --force wlan0
sleep 10
fi
done
Thanks
electrical-banana.com
-
- Posts: 9
- Joined: Tue Oct 23, 2012 4:01 pm
Re: Wifi Reconnect on drop
I did write a post, but discovered from the issues page on github what the issue was (although I thought the fix had been imported - but seems not?) .. basically, I was able to run from the command line myself, but was not running correctly when ran from cron ...
I had to prefix the ifconfig, ifdown etc commands with /sbin/ e.g. /sbin/ifconfig
I had to prefix the ifconfig, ifdown etc commands with /sbin/ e.g. /sbin/ifconfig
Re: Wifi Reconnect on drop
I also discovered this yesterday when I installed the program. I redirected standard error from the script running in cron to a file so I could see what was going on, and saw that it could not find the commands ifconfig, ifdown and ifup. The full path fixed the problem. Another option would be to append /sbin to the PATH in the script.elyobelyob wrote:I did write a post, but discovered from the issues page on github what the issue was (although I thought the fix had been imported - but seems not?) .. basically, I was able to run from the command line myself, but was not running correctly when ran from cron ...
I had to prefix the ifconfig, ifdown etc commands with /sbin/ e.g. /sbin/ifconfig
Unfortunately I don't need the script anymore as I replaced the wifi dongle on my pi late yesterday with a router connected directly through the Ethernet port so I can get more range out of my pi. The added bonus is that I never have to worry about wifi disconnects without reconnects any more if I am out of range of the pi with my smartphone, because I am now hard wired at the pi and that happily keeps on running.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/
https://www.flaminghellmet.com/launch/
-
- Posts: 9
- Joined: Tue Oct 23, 2012 4:01 pm
Re: Wifi Reconnect on drop
Yes, that is what I am thinking of doing at some point. My first Pi runs perfectly with ethernet. I am trialling this with a dongle. As would like to keep current setup. But as this is supposed to control my boiler, it needs to very reliable.pjc123 wrote:... as I replaced the wifi dongle on my pi late yesterday with a router connected directly through the Ethernet port
Also written a shell script to pass control back away from Pi on failure.
Surely there must be a way of having the Pi constantly try and rejoin the wifi network via a config setting?
-
- Posts: 9
- Joined: Tue Oct 23, 2012 4:01 pm
Re: Wifi Reconnect on drop
Well, it seems that my IP address lease is just fine. I've written a ping test and the connection has dropped, yet this script has not kicked in. So, it seems that WiFi connectivity is a dirty, unstable area with Raspberry Pi. My system worked fine in development when in 100% wifi spot, but any chance of a low signal and it's too unreliable.
So, hard wired is the only real choice for a Pi in my opinion.
So, hard wired is the only real choice for a Pi in my opinion.
Re: Wifi Reconnect on drop
I forgot to add that the script did not work for me either. To test it I turned off the router, redirected the stdout and stderr to files and watched the files as the script was running. The problem is that ifconfig does not recognize the dropped connection and just keeps reporting that it is OK via an assigned address. Now iwlist did properly report that it had been a while since there was activity on the connection, but that certainly would not be a solution.elyobelyob wrote:Well, it seems that my IP address lease is just fine. I've written a ping test and the connection has dropped, yet this script has not kicked in. So, it seems that WiFi connectivity is a dirty, unstable area with Raspberry Pi. My system worked fine in development when in 100% wifi spot, but any chance of a low signal and it's too unreliable.
So, hard wired is the only real choice for a Pi in my opinion.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/
https://www.flaminghellmet.com/launch/