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?
1 Answers
What you need here is crontab. Cron jobs are the standard thing when it comes to scheduling tasks at certain time or event instances.
Simply type:
$ crontab -e
and you can place your python scripts in the crontab.
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.
Note
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.
Your Queries
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 bash script.
create a bash script in your folder.
nano myFolder/testConnectuse the following code :
#!/bin/bash wget -q --tries=10 --timeout=20 --spider http://google.com if [[ $? -eq 0 ]]; then echo "Online" else echo "Offline" fipress CTRL+O and then CTRL+X
give it executable rights
$ chmod +x myFolder/testConnectadd this script in your
crontabas mentioned above.
- 1,531
- 2
- 13
- 28
sudo crontab -einstead ofcrontab -ein the terminal because it seems like whatever i have saved in thecrontab -eis not appearing insudo crontab -e. And after reboot, it just doesn't run. BTW, do I need to key in the username and password anywhere to make it work? Also, do I have to make the file executable? – sam Aug 11 '16 at 03:52raspi-configfor the thing to work. But another problem arises, is, considering I want to connect to a monitor now, how come I cannot see any output of the text in the terminal (because my python file will output some texts). Instead, all I can see is just the log in screen asking me for the username and password. – sam Aug 11 '16 at 11:48loggingmodule in Python. That will log your data from a background script and store it in a file you want. Look intologgingin python. it is very good if you actually use it. – Shan-Desai Aug 11 '16 at 11:52tmuxhere. It will start a session and it will be just like your terminal where your output will be shown. Here is the link I wrote an answer for – Shan-Desai Aug 11 '16 at 12:00