Short Description:
I want to auto-start an executable (opencv binary file, generate via c++) via a systemd service-script after booting, but I am unsuccessful.
I narrowed down the error to the code statement "cv::imshow(....)" which opens a window and displays an image. At this point, the code throws the error: "QXcbConnection: Could not connect to display"
However, if I manually execute the sh-script or the binary, both work fine. I searched around stackoverflow for the most common errors, and I tried to fix all I could found. I am quite sure, that:
- My service file actually runs at start (until the error occurred)
- Manually execution of the binary file works fine
- Manually execution of the .sh-script works fine
- I do not have runtime-linking errors (see .sh-script)
I would appreciate any help. Please help me fix the error, and please explain to me, why this error even occurs in the first place. Thanks a lot :)
.
My system:
Machine: Raspberry Pi 3 Model B
Architecture: arm32 / ARMv7
OS: NOOBS
.
My script in /etc/systemd/system/ (test.service):
[Unit]
Description=lalala
[Service]
Type=oneshot
ExecStart=/bin/bash "/home/pi/Desktop/test.sh" start
ExecStop=/bin/bash "/home/pi/Desktop/test.sh" stop
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Moreover, I did execute the following commands:
sudo chmod u+rwx /etc/systemd/system/test.service
sudo systemctl enable test
And if I start the service manually, it runs with the same error output as while autostarting during the boot process:
sudo systemctl enable test
.
My shell script (test.sh):
#!/bin/sh -e
exec 2> /tmp/test.sh.log # send stderr to a log file
exec 1>&2 # send stdout to the same log file
set -x # tell sh to display commands before execution
echo "in script"
start()
{
echo "in start"
sleep 30
LD_LIBRARY_PATH=/usr/local/OpenCV/lib:/usr/local/SFML/lib:/usr/local/curl/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
/home/pi/Desktop/test/main -e &
}
# THE OTHER CASES, NOT PUT IN HERE (stop, status)
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit 0
.
Minimal example of my source code: (executable)
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main()
{
cv::Mat frame;
cv::namedWindow("result", cv::WINDOW_NORMAL);
## CRASH
return 0;
}
.
P.S:
I am aware that there is a similar thread like this (Run OpenCV script on start with imshow). But as there is no solution for this question, and as I have more information to share, I thought it would be more appropriate to start a new thread.