For context, I am using a Raspberry Pi 2 model B, and I am trying to run a continuous python script when the pi boots up. The script uses the GPIO Pins, and with the data it recieves, it changes values of key variables that I am using for my project. I have tried using cron and rc.local, but nothing works for me. It seemed that all of my problems stemmed from the $DISPLAY environment variable, but I couldn't find a way around it. I tried to wait until the variable was set, but still had issues with it, saying that it couldn't connect to the display. But now I am starting to think it may be irrelevant. I am not sure what to do here.
Edit: Some more info was requested and here it is. The way the script changes the values of the variables is through a website link which is hosted by the pi. When it is accessed, the variable state will change. I have put try and except statements into my script so that if it cant access the website, it will try again every 10 secs until it does. This script also isn't a GUI script. This script is meant to run in the background and perform logic with GPIO inputs. I thought I might include this even though I don't think the error has anything to do with it. I am using crontab at the moment to try and run it. The code I am using in crontab is this:
@reboot (sudo idle -r /home/pi/Documents/StatUpdate.py > /home/pi/Documents/logs/logFile.txt 2>&1)
This command can also be run in the command line manually to start the script without error, and the script will work when manually started. The error is this:
Traceback (most recent call last):
File "/usr/bin/idle", line 5, in <module>
main()
File "/usr/lib/python3.7/idlelib/pyshell.py", line 1488, in main
root = Tk(className="Idle")
File "/usr/lib/python3.7/tkinter/__init__.py", line 2023, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
I have also tried to get the pi to wait until the display variable is set using this command: until [ -n "${DISPLAY}" ]; do :; done;
before the sudo idle command. When I do, the error goes away in the log, but it seems that the script still doesn't run. I hope this info helps.
idle
is a GUI program that depends upon tcl/tk. This Q&A may explain more.