0

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 Answer 1

1

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.

  1. create a bash script in your folder.

     nano myFolder/testConnect
    
  2. 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

  3. give it executable rights

        $ chmod +x myFolder/testConnect
    
  4. add this script in your crontab as mentioned above.

8
  • thank you for your great effort for the in depth explanation. It is this kind environment that makes stack overflow so great!
    – sam
    Commented Aug 9, 2016 at 13:37
  • you are welcome. Give a feedback if more help is needed. Update your Question if new queries related to your task arises. Also if you find the above answer after tests make sure to accept it for others to know.
    – Shan-Desai
    Commented Aug 9, 2016 at 13:45
  • Hi @Shan-Desai, the crontab, do I have to put sudo crontab -e instead of crontab -e in the terminal because it seems like whatever i have saved in the crontab -e is not appearing in sudo 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
    Commented Aug 11, 2016 at 3:52
  • @sam when you use sudo crontab you will run your script as superuser. That means it will have all the privileges already. You do not need a password since sudo already on reboot is ready. Give it a try. Maybe your script will have some problem hence it won't run
    – Shan-Desai
    Commented Aug 11, 2016 at 3:56
  • 1
    I hope I am not disturbing you too much. I found out now why the python file doesn't start. Is because I must enable the SSH setting in the raspi-config for 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
    Commented Aug 11, 2016 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.