Pi Restart on disconnection

Hi,

I have a vague idea how this works, could make something myself but I know someone out there will already have something or I’ll have to do some digging as I’m currently too busy to do it myself.

However my PI(s) have been having issues recently with my internet because of issues with my provider I’ve had a bit of an on/off connection - however whenever my internet restarts the PI doesn’t come back online without me unplugging it and restarting it manually. I can’t say I’ve had this issue in the past however I’m sure it never has in the past (or my net has never been off to notice).

So if anyone has anything to restart the PI’s wifi connectivity upon disconnection then feel free. I already have SSH and everything set up so I can control these remotely although this is an issue.

Kind regards,
Matthew

If you search the forms you will find scripts to do this, here is mine.



#!/bin/bash

target="google.com"

count=$( ping -c 3 $target | grep icmp* | wc -l )

if  $count -eq 0 ]

then

date >> /tmp/pingfail.txt

/sbin/shutdown -r now

else

date >> /tmp/pingsuccess.txt

fi


You will need to log into your pi and something like

cd root
nano uptest.sh
past in my code from above and CTRL O then CTRL X to exit
chmod 755 uptest.sh

then add it to your crontab to run

crontab -e
add this line:
*/15 * * * * /root/uptest.sh
then CTRL X and save

pi@raspberrypi ~ $ mkdir restart
pi@raspberrypi ~ $ cd restart
pi@raspberrypi ~/restart $ vi uptime.sh
pi@raspberrypi ~/restart $ chmod 755 uptime.sh
pi@raspberrypi ~/restart $ crontab -e
no crontab for pi - using an empty one
crontab: installing new crontab

Put that cron in, edited it to /restart/uptime.sh - hope for the best.

Cheers for the script, I found one or two that ping every second but I’d rather not waste all my resources pinging a site every second.
Once every 15 minutes is great though, hoping crontab works as myself and cron’s aren’t best of friends.

Checked /tmp/ at 23:00 and there isn’t anything as of yet.

Did you see the file created in your tmp directory yet? If it’s working you should see pingsuccess.txt in there.

If you don’t see it running in the cron run the script by hand ./uptest and make sure it works that way, then troubleshoot why your cron entry didn’t work.

pi@Radar-192-168-0-12 ~ $ cd /tmp
pi@Radar-192-168-0-12 /tmp $ ls
piaware.out piaware.out.yesterday
pi@Radar-192-168-0-12 /tmp $ cd
pi@Radar-192-168-0-12 ~ $ cd restart
pi@Radar-192-168-0-12 ~/restart $ ./uptime.sh
pi@Radar-192-168-0-12 ~/restart $ cd /tmp
pi@Radar-192-168-0-12 /tmp $ ls
piaware.out piaware.out.yesterday pingsuccess.txt

Must have an issue with the cron, which is the same as you posted.

Is cron running?


ps -ef | grep cron | grep -v grep

It appears it wasn’t, I have managed to fix this though and it does appear to be logging; however could I set it as “192.168.0.1” as my own router or? Not like it’s a major issue sending a ping to google.

Most likely you own router will still respond when the internet is down, its doubtful your router stops responding too. I would keep it using Google personally.

Glad you got it all working now :slight_smile:

Im getting the following error

wc: invalid option – ‘1’
try 'wc --help" for more information
./uptest.sh: line 7 : -eq: unary operator expected.

Here is contents of uptest.sh

GNU nano 2.2.6 File: uptest.sh

#!/bin/bash

target=“google.com

count=$( ping -c 3 $target | grep icmp* | wc -1 )

if $count -eq 0 ]

then

date >> /tmp/pingfail.txt

/sbin/shutdown -r now

else

date >> /tmp/pingsuccess.txt

fi

Did you copy and paste into nano from my code above? It looks like maybe it split it on multiple lines? Line 7 should be the shutdown line so your error tells me your script has too many lines.

removed lines. Still get

pi@raspberrypi ~/restart $ ./uptest.sh
wc: invalid option – ‘1’
Try `wc --help’ for more information.
./uptest.sh: line 4: : -eq: unary operator expected

uptest.sh now reads

#!/bin/bash
target=“google.com
count=$( ping -c 3 $target | grep icmp* | wc -1 )
if $count -eq 0 ]
then
date >> /tmp/pingfail.txt
/sbin/shutdown -r now
else
date >> /tmp/pingsuccess.txt
fi

also cron isnt running so thats another issue. When I did crontab -e it used an existing cron under tmp

/tmp/crontab.BdD8qg

does it need to ‘shutdown -r now’

could it do ‘sudo service network restart’ (don’t know if that works on Pi - it does on RedHat)

then restart the the other services if required (dump 1090, piaware, fr24feed, etc)