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:
# base-image for python on any machine using a template variable,
# see more about dockerfile templates here: https://www.balena.io/docs/learn/develop/dockerfile/
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 \
python3-guizero
# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt
# pip install python deps from requirements.txt on the resin.io 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.
ENV UDEV=1
# main.py will run when container starts up on the device
CMD ["bash", "src/startx.sh"]
#!/usr/bin/bash
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) &
fi
# start desktop manager
echo "STARTING X"
startx
# 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…
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 startx.sh file, but no luck on the touch rotation:
export ROTATE_DISPLAY=right
export DISPLAY_ROTATE_TOUCH=right
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.
Hi!
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/file.py in autostart.