13

I have the following problem: Using a RaspPi 3B running Raspbian Jessie (version 4.4.26-v7+) I would like the GUI of a self-written python script to be executed on startup.

I know that this question has been asked on this website as well as on others, but as Raspbian keeps changing and none of the solutions suggested before worked for me (see below), I would like to ask it again.

From my understanding, I need to load XServer and execute my script afterwards. This should happen instead of loading the whole desktop.

One way of achieving this is supposed to be via the LXDE autostart file (as outlined in this answer). I edited the file /etc/xdg/lxsession/LXDE-pi/autostart as well as the file /etc/xdg/lxsession/LXDE/autostart (the former originally contained four lines instead of three as suggested in the post referenced) - adding the lines @sudo python /full/path/to/file.py and @openbox, but my Pi still boots into the desktop and does not automatically execute the script at all. Annotation: I tried just the script line and the script line together with the openbox line for either one of the files and for both files together.

Another strategy involves the rc.local file (as suggested in the official documentation). However, only adding python /full/path/to/file.py & did not work (as there still was my GUI missing) and further adding startx didn't help as I apparently could not link the startx with the python script and the former therefore closed immediately after it started.

Some suggest, that (additionally) .xinitrc should be used. This, however, did not work either:

su -c python /full/path/to/file.py pi

And, last but not least, in some way, it should be doable using init.d - but I haven't found details on this, yet.

I would really appreciate any hint towards the right direction. Where is my mistake? How should my line in rc.local or .xinitrc look like? Or have there been major changes related to the switch to pixel and I should try something else?


Some additional information: The script needs sudo rights. It is written in Python 2.7, the GUI is using Tkinter. The GUI fills the screen completely, so backgrounds are irrelevant. The script is supposed to run forever (or until it is stopped by user input through the GUI) and uses system services like wifi and ethernet.


More information: All is happening locally.


Clarification: By now I spent more than 8 hours searching the net, trying out tutorials from different starting points or mixing them together. I came to the conclusion, that either my script (which works perfectly when started in pixel) does not work if run in a non-desktop environment (which I doubt) or that a new tutorial is needed, because the October 2016 changes to Raspbian made any older tutorials obsolete. Furthermore, the challenge lies not in starting the script or starting Xserver (or something similar) independently from each other, but in doing both linked so that the script will use the display created by Xserver.

12
+50

For the past month or so I've been working on basically the exact same thing, so I've researched how to do this a lot and know how to do it with the latest version of Raspbian (PIXEL).

nodm is a minimal display manager that bypasses loading LXDE, and openbox (which is already installed on the Pi) provides a minimal session manager and works with the X server.

To set up this environment in Raspbian, install nodm with apt-get and edit the file /etc/default/nodm. You need to set the option NODM_ENABLED to true and NODM_USER to pi (or whatever your username is).

Then create a custom Xsession file in your home folder (/home/pi/.xsession) with the following contents (the while loop isn't necessary, it just automatically restarts the Python script if it crashes):

#!/usr/bin/env bash
exec openbox-session &
while true; do
  python3 /home/pi/Documents/script.py
done

and this should be all that is necessary I think. I wrote a Bash script to set this up automatically:

sudo apt-get -y install nodm

# Edit nodm config file
sudo sed -i -e "s/NODM_ENABLED=false/NODM_ENABLED=true/" -e "s/NODM_USER=root/NODM_USER=pi/" \
  /etc/default/nodm

# Create custom Xsession file
printf "%s\n" \
  "#!/usr/bin/env bash" \
  "exec openbox-session &" \
  "while true; do" \
  "  python3 $PWD/main.py" \
  "done" \
  > /home/pi/.xsession

Notes:

  • I found this link helpful, but some of the information is outdated now: https://blog.qruizelabs.com/2014/04/29/raspberrypi-kiosk-matchbox-uzbl/ They use Matchbox window manager, which didn't work for me because I needed multiple window support, but it might be a nice option if you don't.
  • The solution above worked, but I wanted an environment as light as possible, so I switched to Raspbian Lite which has no GUI and started from scratch only installing the packages that are absolutely necessary. The process is pretty similar, but in addition to nodm you need to install xserver-xorg, xinit, openbox, and any other dependencies your script has. Then if you are using Openbox, instead of running the Python script in the Xsession file, the part of the code that runs it needs to be moved to a separate script (/home/pi/.config/openbox/autostart). I can elaborate more on how to do this if desired.
  • Hi tjohnson, I used the method you described above on Raspbian and got it working, thank you! I wanted to try doing in on Raspbian Lite also you mentioned in your last note. I installed the extra dependencies you listed but the app didn't start. I assume it's related to your last comment about moving that to a separate script. I don't have a home/pi/.config/openbox folder so I created it and the autostart file but it doesn't seem to be working. Could you please elaborate on that? Thanks – AngeloQ Jan 5 at 16:40
1

The autostart file exists in several different places. It is simply a matter of editing the right autostart file at the right path. I tried editing /home/pi/.config/lxsession/LXDE-pi/autostart. That finally did the trick for me.

/etc/xdg/lxsession/LXDE-pi/autostart seems to be the wrong path.

0

The dirty trick I've used on RPi in the past was to put commands in my /etc/rc.local file just before the exit 0 line. In your case I'd try the following line and reboot.

python /full/path/to/file.py &

As you've stated that sudo level permissions are needed you may have to instead adjust the su command you've previously tried because right now it looks like you're trying to run under the pi user.

One warning about my suggestion of rc.local file usage, if your script exits with non zero status you'll not complete the boot so to be safer during tests you can try the following to ensue exit still equals 0

python /full/path/to/file.py & || exit 0

Edits and updates

Looking though some search data, found an instructable that demonstrates using a launcher script referenced in cron tab to facilitate loading python scripts. May seem round about to load cron to load a sh/bash script in order to load python but... it is convoluted enough to make some twisted sense.

  • Comments are not for extended discussion; this conversation has been moved to chat. – Ghanima Nov 11 '16 at 22:40
0

So depending on what input your code needs this solution may work.

The first thing I did was make a script in my / directory that goes like this:

#!/bin/bash
echo "Starting program..."
cd /home/pi/myFolder
sudo python3 myPython.py $@ #$@ takes all arguments and passes them to python.
exit 0

You have to make that runnable with chmod -x scriptName.sh.

Then do crontab -e in the terminal and add @reboot sudo bash /scriptName.sh

Set your boot option to CLI and you should be good to go! This worked on my 3B running Raspbian.

One caveat, if you're using a touch screen for your kiosk/screen I have yet to figure it out. It will work fine with mouse input but as for touch input, at least for the screen I'm trying to use, it will be off in a way that makes it unusable.

Note: If your code is doing file IO like mine you must use cd to navigate to your code's location in your launcher script or python will assume its location is that of your script and will perform file IO accordingly.

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.