Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
I admit that it is not of much help, but maybe it's worth looking how e.g. XBMC (or other HTPC dicrtibution) boots? Alternatively, maybe this would answer your question raspberrypi.stackexchange.com/questions/11866/… – Petr Gladkikh Nov 13 '16 at 15:09
up vote 5 down vote accepted
+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.
share|improve this answer

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.

share|improve this answer
    
Comments are not for extended discussion; this conversation has been moved to chat. – Ghanima Nov 11 '16 at 22:40
    
Thank you @Ghanima, that was fancy. – S0AndS0 Nov 11 '16 at 22:49

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 finally FINALLY!!!!! did the trick for me.

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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