0

I let my python script run at startup but some of HTTP requests (using requests pip module) not working. However, when I run the script manually with sudo openvt -s -w /path/to/launcher.sh, all HTTP requests are working fine.

this is my code in~/.bashrc

sudo openvt -s -w /path/to/launcher.sh

and in launcher.sh, it is as simple as cd to directory and run python script.

cd /path/to/python/script
python start.py

when the script is run at startup. HTTP requests throw this error:

ConnectionError: HTTPSConnectionPool(host='', port=443): Max retries exceeded with url: /my/api (Caused by <class 'socket.gaierror'>: [Errno -3] Temporary failure in name resolution)

Again, when I run the script via ssh using this command sudo openvt -s -w /path/to/launcher.sh, everything is working fine. I'm struggling this bugs for days. Thank you so much for any helps :)

2

Three things you can try that can help:

  1. Delay the Pi startup till the network is connected.

    sudo raspi-config Option 3 Option B2

  2. Set the Pi to have a static IP address and define the DNS server on your network by IP address

  3. Add a short delay to the start of the Python program.

Be aware that setting two can leave the Pi sitting around for awhile...

What you are seeing is not actually a bug more a 'quirk' - basically the DNS service is not fully up and running by the time you make the first request so it reports a 'not found' and your program dies.

A couple of comments if you do not mind though:

  • Having any Pi auto-log on is not a good idea from a security point of view. It sets a bad habit to get into :). Learn to use systemd - it's a pain when you start but the way forward (it's only been around 10 years now) and folk still argue over it.
  • Any code should be error trapped esp when you are talking across networks. Even if the program retires indefinitely it's better than aborting.

I've added a couple of more tags to the question.

| improve this answer | |
  • thanks a lot @Andyroo i solved my problems with infinite retries and change DNS server to 8.8.8.8 and 8.8.4.4 Thanks for pointing out to systemd :) – Pakpoom Tiwakornkit Jun 12 at 12:05
1

.bashrc is NOT intended to run scripts. It DOES NOT run on boot!

It is run each time a non-login interactive shell is started and is used to configure the shell.
~/.bashrc: executed by bash(1) for non-login shells.

See rc.local although the ONLY reliable method is systemd

| improve this answer | |
  • I configured raspberry pi to boot to console with auto login. so my script is run but has problems with some HTTP requests. If .bashrc is not intended to run scripts. which else should I use ? thanks @Milliways – Pakpoom Tiwakornkit Jun 9 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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