Random wifi dropout rpi3

I’ve been having issues with wifi dropping out when Kore remote connects to OSMC after long periods of inactivity. I’ve found using iw to turn off power save doesn’t neccesarily fix the problem so I wrote a shell script that I run with cron every minute to see what’s going on, test connectivity to my home network and restart connman when it notices an issue. It also checks to see if power save is on and turns it off if it isn’t already.

Here is the script:

#!/bin/bash
log="/home/osmc/log/power_save"
psv=`sudo iw dev wlan0 get power_save`
status=${psv##* }
stamp=`date`
net_status=`connmanctl services`
packet_loss=`ping -w 5 192.168.1.1 | grep 'packets received' | awk '{print $7}' | sed 's/\%//'`
if [ $packet_loss -gt 0 ]; then
pkt_loss="Packet Loss: "$packet_loss"%.  Restarting connman"
`sudo systemctl restart connman`
else
pkt_loss="Packet Loss: 0%"
fi
echo '  '$stamp >> $log
echo $net_status >> $log
echo $pkt_loss >> $log
if [ $status = "on" ]; then
echo "power save on, set to off" >> $log
`sudo iw dev wlan0 set power_save off`
else
echo "power save already off" >> $log
fi
echo "" >> $log

I know its ugly code but so far it’s been working.

THIS IS PERFECT!!! been happening to me since ages… so where/how do I use this? Thanks!!