-2

I want to update data to thingspeak using my raspberry pi.so that, I run my python code and I set it to run at reboot using Crontab. while its running at the background my wifi connection gets down and so the script stops sending data to cloud, even after the network comes up.

Is there any way to restart the .py file when network gets down or up automatically.

fayaaz
  • 11
  • 2
  • I wrote a code to pop up ubuntu notification whenever the internet is down or when the internet is back again. Have a look at it and feel free to use it according to your use. URL: https://github.com/abhilashgoyal/python_learn/blob/master/net_notify.py – abhilash_goyal Apr 25 '18 at 06:22

1 Answers1

0

You could try a loop that pings a service that you know is always up, or your gateway/router then restart the .py script when the reply comes back up. (eg, ping google.com if no response halt and attempt to get the connection back in what ever method you wish) then once you have a response again restart the script. something like

loop_value = True
while loop_value:
        try:
                urlopen("http://google.com")

for the restart process you could use either like subprocess (this one would work better in a case where it was a separate script) or os.execv.

Justin
  • 1
  • 1