Autostart python code in X Server


I’m running x server on my Pi 3b+ and I would like to start an python code when the desktop starts.
I have tried the:
COPY autostart /etc/xdg/lxsession/LXDE-pi/autostart
command, and my autorun file contains:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash

python3 /usr/src/app/src/

But it does not work. Any hints?

Hi, it may help for you to share the Dockerfile with us as well. Perhaps something there is preventing the autostart script from running.

Hi, @bucknalla

Here’s my docker file:

# base-image for python on any machine using a template variable,
# see more about dockerfile templates here:
FROM balenalib/%%BALENA_MACHINE_NAME%%-debian-python:latest

# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git

# Set our working directory
WORKDIR /usr/src/app

RUN pip install --upgrade pip

RUN apt-get update
RUN apt-get install -y python-tk python3-tk tk-dev
#RUN apt-get install libhidapi-hidraw0 libudev-dev libusb-1.0-0-dev

RUN install_packages xserver-xorg \
  xserver-xorg-input-evdev \
  xinit \
  xfce4 \
  xfce4-terminal \
  x11-xserver-utils \
  dbus-x11 \
  matchbox-keyboard \
  libhidapi-hidraw0 \
  libudev-dev \
  libusb-1.0-0-dev \
  python3-urllib3 \

# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt

# pip install python deps from requirements.txt on the build server
RUN pip install -r requirements.txt
RUN pip install --upgrade Pillow

# This will copy all files in our root to the working  directory in the container
COPY src ./src/

COPY autostart /etc/xdg/lxsession/LXDE-pi/autostart

# Enable udevd so that plugged dynamic hardware devices show up in our container.

# will run when container starts up on the device
CMD ["bash", "src/"]


what does your do exactly?

i think there is a possiblity the autostart file is not executable?
perhaps you can run

RUN chmod +x /etc/xdg/lxsession/LXDE-pi/autostart

after the COPY autostart /etc/xdg/lxsession/LXDE-pi/autostart command?

let us know


Hi @rahul-thakoor contains this code:


export DISPLAY=:0.0
export DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket

export ROTATE_DISPLAY=right

# rotate screen if env variable is set [normal, inverted, left or right]
if [[ ! -z "$ROTATE_DISPLAY" ]]; then
  echo "YES"
  (sleep 3 && DISPLAY=:0 xrandr -o $ROTATE_DISPLAY) & 

# start desktop manager

# uncomment to start x without mouse cursor
# startx -- -nocursor

# uncomment to open an application instead of the desktop
# startx xterm  

This code rotates the screen, but unfortunately not the touch functionality. I will have to look in to this too…

thanks. let us know if using chmod works

as for the touch rotation,you can check our implementation in our xserver block which has an env var to control touch rotation GitHub - balenablocks/xserver: A simple X11 server block

hope this helps

No luck with the chmod command.

Sometimes I get a “Can’t open display :0” error, and the only way to get i back is to reboot the OS.
The error appears on the “sleep 3 && DISPLAY=:0 xrandr -o $ROTATE_DISPLAY” line.

Tried setting this in the file, but no luck on the touch rotation:
export ROTATE_DISPLAY=right

I’d love to know the answer to this too!

We sleep for 10 seconds to wait for startx before running the rest of our commands: interactive-label/ at main · ACMILabs/interactive-label · GitHub

We also had trouble with the value of DISPLAY, see our solution just below startx sleep - hope it helps you too.

Something that jumps out at me is that you’re calling xrandr before startx. As far as I know, X must already be running for xrandr to work. Manually setting the DISPLAY variable may be masking the issue here, since it may trick processes into thinking there is a X display to work with.

I think it would be useful to comment out the line sleep line until you have the autostart stuff working. Then, try just putting a litmus test in autostart. Maybe something like touch /tmp/autostarting, so you can check for the existence of that file as confirmation the script is running. I think maybe the path you’ve used is not valid, but it would be worthwhile establishing a reliable test case first so you can fault find effectively.

I have managed to get a python script to start automatically in an image is based on balena’s X11 window manager demo by starting the script with #!usr/bin/env python3, chmod +x’ing it and putting /path/to/ in autostart.