I'm having an issue that hopefully someone will be able to help me with. I have a Raspberry Pi 2 running OSMC, and I run my traffic through a VPN using the openvpn service. Every so often my pi loses internet connection, and if I attempt to ping a web address it just times out with 100% packet loss. However, if I then restart the openvpn service it will reconnect and everything will be fine.
At the moment I'm checking manually, but it would be much easier if I could set up a script to run with cron every 5 minutes which would automatically ping a web address and restart the service. So far I have a script which I can modify to either reboot the entire Pi or to restart the wifi, but since I run on ethernet and I would prefer not rebooting the entire Pi (could be watching videos etc) these aren't ideal. The script I have at the moment is:
ping -c4 8.8.8.8 > /dev/null
if [ $? != 0 ]
then
sudo /sbin/shutdown -r now
fi
So basically all I want to do is change where it says sudo /sbin/shutdown -r now
to restart the openvpn service. Is it as simple as just changing it to sudo service openvpn restart
?
As a follow up, there's a script in a similar question here which checks ten times if restarting the wifi has worked and then reboots the Pi if not. Would it be easy to modify that script to restart the service ten times rather than the wifi?
Thanks for your help, I'm sure this is easier than I've made it sound but I prefer to give more information rather than less.
(Here's the code from the related question, just wanted to make sure I gave credit earlier on. I would obviously change the TESTIP section to 8.8.8.8)
#!/bin/bash
LOGFILE=/var/log/wifitestlogfile.log
TESTIP=74.125.224.72
ping -c4 ${TESTIP} > /dev/null
if [ $? != 0 ]
then
message="$(date) -- WiFi seems down, restarting - message from script /usr/src/scripts/wifi_test_2.sh"
# echo >$LOGFILE empties the file so just the last log is saved
echo $message >>$LOGFILE
ifdown --force wlan0
ifup wlan0
downcount=$(grep down /var/log/wifitestlogfile.log | wc -l) # counts the times the letters "down" are in the logfile and passes it to the variable doencount
echo ${downcount} >>$LOGFILE
if ((${downcount} >= 60 )); then
echo >$LOGFILE # empties the file
sudo shutdown -r now
fi
else
message="$(date) -- WiFi seems up - message from script /usr/src/scripts/wifi_test_2.sh"
# echo >$LOGFILE empties the file so just the last log is saved
echo $message >>$LOGFILE
fi
sudo service openvpn restart
in your first code snippet not work?