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