Spalunkeldusch
Posts: 16
Joined: Sat Dec 08, 2018 6:52 pm

run python script on boot issue

Fri Mar 15, 2019 10:42 pm

Hello,

I have a python script that displays text on my i2c OLED screen connected to the Pi. It asks to attach the RFID card to the reader. After the card is attached the screen displays the card ID and other data. When I try to run this script via default raspbian text editor - it doesn't work. I get a missing library error or something, however, when I run in it in a terminal with sudo python scriptxx.py it works as intended.

What I want to do is to run that script automatically when i plug my raspberry to a power source. I have tried using this guide's https://www.dexterindustries.com/howto/ ... t-startup/ options: rc.local; .bashrc; crontab; however it was not able to produce the result I wanted. Usually, after I execute everything as shown in mentioned tutorial and reboot the raspberry, my i2c OLED screen gives me random pixels whole over the screen. Is it because it is unable to run the script using text editor like I have mentioned before?

Is there a way to just run my script on boot without having to manually start it every time my Pi boots to prevent any errors?
If so, could you please give me a step by step guidance?

Much appreciated!

User avatar
thagrol
Posts: 1700
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: run python script on boot issue

Sat Mar 16, 2019 12:50 pm

What libraries are you importing and where have they been installed?

You may find that setting the current working directory before calling your script (or as the first thing your script does - see the os module) cures the problem.

For example, start your script with:

Code: Select all

#!/usrbin/env python
import os
os.chdir('/path/to/directory_containing_script')

# rest of code goes here
Should work with python 3 too
This space unintentionally left blank.

Spalunkeldusch
Posts: 16
Joined: Sat Dec 08, 2018 6:52 pm

Re: run python script on boot issue

Sat Mar 16, 2019 2:39 pm

thagrol wrote:
Sat Mar 16, 2019 12:50 pm
What libraries are you importing and where have they been installed?

You may find that setting the current working directory before calling your script (or as the first thing your script does - see the os module) cures the problem.

For example, start your script with:

Code: Select all

#!/usrbin/env python
import os
os.chdir('/path/to/directory_containing_script')

# rest of code goes here
Should work with python 3 too
Thank you for you answer.
I am currently using these libraries. They are located in /home/pi/ i assume, and the project script is located in /home/pi/Downloads, is that an issue?

Code: Select all

#!/usr/bin/env python ---just added this after reading your answer.
import os
os.chdir('/home/pi/Downloads/') ---just added this after reading your answer.
import RPi.GPIO as GPIO
import sys
sys.path.append('/home/pi/MFRC522-python')
import SimpleMFRC522
import datetime
import requests
import time

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

import subprocess
When I try to run the script using "Thonny" editor I get the following error:
Traceback (most recent call last):
File "/home/pi/Downloads/script.py", line 12, in <module>
import Adafruit_GPIO.SPI as SPI
ImportError: No module named 'Adafruit_GPIO'
If I run it via terminal, by going to script location -> cd Downloads; then -> sudo python script.py ----- it works as intended.

I have tried to add my script location to /etc/rc.local; I have added it before 'exit 0' command and my line was

Code: Select all

/home/pi/Downloads/script.py &
& added at the end of the line since my script loops over and over again. I have also tried making my script executable with chmod +x script.py - didn't work as well. Also tried to type in rc.local line like this: sudo python /home/pi/Downloads/script.py & - didn't work as well.


after I reboot the Pi I always get this image on the OLED itself:
Image

What I have also noticed, that after I reboot my Pi and launch the script manually via terminal - it launches properly - display is showing what is intended to be shown, however it doesnt react to my RFID card attachment to the reader. After I attach the card to the reader I should get a response on the screen with details. However it just displays the main screen. After I restart the script manually - it starts working properly. Weird :/

Anything you could help with?

User avatar
thagrol
Posts: 1700
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: run python script on boot issue

Sat Mar 16, 2019 5:35 pm

Spalunkeldusch wrote:
Sat Mar 16, 2019 2:39 pm
When I try to run the script using "Thonny" editor I get the following error:
Traceback (most recent call last):
File "/home/pi/Downloads/script.py", line 12, in <module>
import Adafruit_GPIO.SPI as SPI
ImportError: No module named 'Adafruit_GPIO'
That's a pretty clear indication of what's wrong. At least when running from thonny. Unfortunately I don't use it but be aware that thonny has a built in python engine that it uses in preference to the system one.

Thonny also uses python 3 not python 2 (aka python), that can make a difference in which version of a library is needed.

When you ran thonny what was the current working directory? If youu started it via the desktop menu it's unlikely that it is "/home/pi/Downloads" so it can't find the library. Unless you've done a formal install of the library it won't be anywhere on the default $PYTHONPATH
If I run it via terminal, by going to script location -> cd Downloads; then -> sudo python script.py ----- it works as intended.
because the current working directory is the one that contains both the script and the library

I have tried to add my script location to /etc/rc.local; I have added it before 'exit 0' command and my line was

Code: Select all

/home/pi/Downloads/script.py &
& added at the end of the line since my script loops over and over again. I have also tried making my script executable with chmod +x script.py - didn't work as well. Also tried to type in rc.local line like this: sudo python /home/pi/Downloads/script.py & - didn't work as well.
[/quote]
First thing to do is to change that to the one below. As things are you're losing all output and error message from your script. The failure may very well be for a different reason.

Code: Select all

 python /home/pi/Downloads/script.py > /tmp/script.log 2>&1 &
"sudo" is not needed as rc.local is already run by root. Once that changes has been amde, reboot and look at the contents of /tmp/script.log to see what error/exception is actually occuring.
after I reboot the Pi I always get this image on the OLED itself:
Image

What I have also noticed, that after I reboot my Pi and launch the script manually via terminal - it launches properly - display is showing what is intended to be shown, however it doesnt react to my RFID card attachment to the reader. After I attach the card to the reader I should get a response on the screen with details. However it just displays the main screen. After I restart the script manually - it starts working properly. Weird :/

Anything you could help with?
Not with those last items. I don't have your hardware. When you launch it manually did you cd to its directory first or just run, say,

Code: Select all

python /home/pi/Downloads/script.py

A couple of other points:
  • As I mentioned above, thonny runs python3 "python" runs your code under python 2. There are differences but I don't know if they'll be critical to your code.
  • When calling your script using "python ..." the shebang (#!...) isn't needed, nor is execute permission.
  • The user running the python interpreter must have read permission on your code and any custom libraries you're using.
This space unintentionally left blank.

klricks
Posts: 6499
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: run python script on boot issue

Sat Mar 16, 2019 7:23 pm

If the script needs to be run as user pi then try this in rc.local

Code: Select all

sudo -u pi python /home/pi/Downloads/script.py &
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

User avatar
thagrol
Posts: 1700
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: run python script on boot issue

Sat Mar 16, 2019 8:19 pm

klricks wrote:
Sat Mar 16, 2019 7:23 pm
If the script needs to be run as user pi then try this in rc.local

Code: Select all

sudo -u pi python /home/pi/Downloads/script.py &
Or skip rc.local entirely and put it in the pi user's crontab.

Either way, until we have the actual error message you're getting when starting your script at boot there ain't a whole lot more that we can do.
This space unintentionally left blank.

Spalunkeldusch
Posts: 16
Joined: Sat Dec 08, 2018 6:52 pm

Re: run python script on boot issue

Sun Mar 17, 2019 6:55 pm

thagrol wrote:
Sat Mar 16, 2019 8:19 pm
klricks wrote:
Sat Mar 16, 2019 7:23 pm
If the script needs to be run as user pi then try this in rc.local

Code: Select all

sudo -u pi python /home/pi/Downloads/script.py &
Or skip rc.local entirely and put it in the pi user's crontab.

Either way, until we have the actual error message you're getting when starting your script at boot there ain't a whole lot more that we can do.
I have managed to bypass the library error by moving to library directory and running

Code: Select all

sudo python3 setup.py install
Now, I have it running properly without errors even with Thonny.

Now, I have executed two commands:

Code: Select all

sudo crontab -e
At the end of crontab window I have typed in:

Code: Select all

@reboot /home/pi/Adafruit_Python_SSD1306/examples/script.py & > /home/pi/log.txt
After reboot, the script starts properly on my OLED, however it does not respont to my RFID reader like I have mentioned previously. This happened before, when I restarted the Pi, started the script via Terminal, it started properly however was not functional. Then I just had to CTRL+C to stop it and launch it again and everything started working as inteded.

The log file I have generated is empty (?).
Anything?

User avatar
thagrol
Posts: 1700
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: run python script on boot issue

Sun Mar 17, 2019 8:43 pm

Spalunkeldusch wrote:
Sun Mar 17, 2019 6:55 pm
After reboot, the script starts properly on my OLED, however it does not respont to my RFID reader like I have mentioned previously. This happened before, when I restarted the Pi, started the script via Terminal, it started properly however was not functional. Then I just had to CTRL+C to stop it and launch it again and everything started working as inteded.
Sorry, no suggestions on that. I don't have the hardware or your code.
The log file I have generated is empty (?).
That could be correct. I can't say for certain without seeing the code. If there are no "print" statements there won't be anything written to stdout. Be aware that ">" only redirects stdout. As error messages go to stderr any that occur won't be captured. If you want to capture any error messages change your crontab entry to

Code: Select all

@reboot /home/pi/Adafruit_Python_SSD1306/examples/script.py & > /home/pi/log.txt 2>&1
That will capture any error text taht occurs into the same log file.
This space unintentionally left blank.

Return to “Beginners”