Adding a QT Program to Boot Auto-Start
This document describes how to make a QT program start automatically at boot by modifying systemd.
1 Create the systemd configuration file
Under the /etc/systemd/system/ directory, create a service configuration file — here using qt_display.service as an example.
# 在 /etc/systemd/system/ 目录下,创建服务配置文件
sudo vim /etc/systemd/system/qt_display.serviceConfiguration file example
# qt_display.service
[Unit]
Description=Qt Display Application
After=graphical.target
Requires=graphical.target
Wants=graphical.target
[Service]
Type=simple
ExecStartPre=/bin/sleep 10
ExecStart=/home/linaro/Desktop/ad_test/build-ad_display-Desktop-Debug/ad_display -d ad7606 -m 1
User=root
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/root/.Xauthority"
Environment="LD_LIBRARY_PATH=/home/linaro/Desktop/qwt-6.1.4/build/lib:$LD_LIBRARY_PATH"
Restart=on-failure
RestartSec=3
[Install]
WantedBy=graphical.targetMeaning of each parameter
[Unit]
Description= service description
After= the target or service after which this service should start
Requires= a target or service that must be active for this service to start
Wants= a target or service this service would like to have active, but does not strictly require
[Service]
Type= service type
ExecStartPre= command/script to run before the main process starts
ExecStart= command to run to start the service — the main process
User= user identity under which the service runs
Environment= environment variables
Restart= restart policy for the service
RestartSec= delay before restarting
[Install]
WantedBy= the target under which this service is started
2 Start the system service
Reload the system services, then enable and start the service.
# 重新加载系统服务
sudo systemctl daemon-reload
# 启用服务
sudo systemctl enable qt_display.service
#启动服务
sudo systemctl start qt_display.serviceCheck whether the service started normally. If it did, reboot the system to verify that the service auto-starts at boot. If there is a problem, run the following commands to inspect the error.
# 检查服务状态
sudo systemctl status qt_display.service
# 查看系统日志
journalctl -u qt_display.service3 Other common commands
# 禁用系统服务
sudo systemctl disable qt_display.service
#停止服务
sudo systemctl stop qt_display.service