Adding a QT Program to the Boot Auto-Start Service
This document describes how to modify systemd to auto-start a QT program at boot.
1 Create the systemd Configuration File
In the /etc/systemd/system/ directory, create a service configuration file. Take 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 in the configuration file
[Unit]
Description=Service description
After=The target or service after which this service starts
Requires=This service requires a target or service to be active before it can start.
Wants=This service wants a target or service to be active, but does not enforce it.
[Service]
Type=Service type
ExecStartPre=Commands and scripts to complete before the main process starts
ExecStart=Command to execute to start the service — main process
User=User identity under which the service runs
Environment=Set environment variables
Restart=Restart requirements for the service
RestartSec=Restart delay
[Install]
WantedBy=Specifies under which target this service is started
2 Start the System Service
Reload system services, 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 is normal, restart the system and verify whether the service auto-starts at boot. If there is a problem, run the following commands to query 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