QXcbConnection: Could not connect to display within a service
0
votes
1
answer
146
views
I have a shell script that sets environment variables and runs an executable. It looks like this:
export PATH=$PATH:/some/extra/binaries
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/some/extra/lib
cd /path/to/execuatble
DISPLAY=:0.0 nohup ./app &
When I run the shell script as as user from the terminal it encounters no problem. However I also wrote a service file that runs a python script which among other things launches this shell. Example python file:
import subprocess
import sys
import logging
logger = logging.getLogger(__file__)
logger.setLevel(logging.INFO)
try:
result = subprocess.run(["/path/to/shell/script/script.sh"], universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
logger.info(result)
except Exception as e:
logger.info("Error starting process: " + str(e), e)
The python file is running within a systemd service with a service file:
[Unit]
Description=App runner Service
After=network.target
[Service]
Environment=PYTHONUNBUFFERED=1
ExecStart=/usr/bin/python3 /path/to/python/script/script.py
Restart=always
RestartSec=5
User=root
[Install]
WantedBy=multi-user.target
When I run the service, I look at what it does with journalctl -f -u servicename
, and I can see the shell script failing when I try to run the application with
No protocol specified \n QXcbConnection: Could not connect to display :0.0
What is the root cause and how can I fix it? I'm open to changes of either the shell script, python, or the service file.
I suspect it has to do with the fact that systemd run this as root. BTW, I have no idea why would I even need an X server since this app doesn't have any GUI. Maybe it has to do with the fact that it uses Qt (we used Qt for a non-gui app since we wantd an event loop and to utilize the signal /slot mechanism to pass events between threads and objects easily.
If this is relevant it runs on Ubnuntu 16.04
Asked by Yair M
(103 rep)
Feb 19, 2025, 09:22 PM
Last activity: Mar 8, 2025, 03:09 PM
Last activity: Mar 8, 2025, 03:09 PM