I have got a headless raspberry pi here. My python file requires connection to internet to scrap some info. However I do not have continuous internet connection and power supply to my raspberry. Instead, it will only be powered for a certain day and time only. If I set up the few WAP with the corresponding password in the raspi, and using a USB wireless adapter. Is there issue of sequence here? Since it is possible to have WiFi signal first before the headless unit is powered on, or the other way around. And how to set the python file to run 5 seconds only after it has completely booted up and have internet connection? What's is the code to check for Internet availability?
What you need here is
crontab. Cron jobs are the standard thing when it comes to scheduling tasks at certain time or event instances.
$ crontab -e
and you can place your python scripts in the
For instance, If you want the python script to run everytime your Pi reboots then :
$ crontab -e ## inside your crontab file # this line below will trigger the file on Pi's reboot @reboot /usr/bin/python3 /home/pi/myFolder/myscript.py
@reboot handle takes into consideration that
myscript.py will trigger once the Pi is rebooted.
You can schedule tasks very 5 Minutes or every 12 hours or Sundays at 12:00.
Here is a link which will help you with
crontab Cron Jobs on RPi
If you are the programming geek type you can use schedule PyPi Module where you can schedule tasks by programming it.
Since you mention that the Power supply may not be constant, chances are that your time on your Pi will start varying to your real time. Example, if today is Tuesday 12:00 PM and if you Pi was off for some time then the time on your Pi may vary Monday 14:00 PM in this case you can use your Network's NTP server to sync back the time or use an RTC module which maintains the time even when the Pi is switched off.
Since it is possible to have WiFi signal first before the headless unit is powered on, or the other way around.And how to set the python file to run 5 seconds only after it has completely booted up and have internet connection?
As far as I know the
SSH is enabled automatically on Pi's boot depending on if you have enabled
ssh in your
raspi-config. If you want to add a wait period try adding:
@reboot sleep 5; /usr/bin/python3 /home/pi/myFolder/myscript.py
What's is the code to check for Internet availability?
for this you might need a
create a bash script in your folder.
use the following code :
#!/bin/bash wget -q --tries=10 --timeout=20 --spider http://google.com if [[ $? -eq 0 ]]; then echo "Online" else echo "Offline" fi
press CTRL+O and then CTRL+X
give it executable rights
$ chmod +x myFolder/testConnect
add this script in your
crontabas mentioned above.