02 QT program added automatic startup service
This document will introduce how to modify systemd to automatically start the QT program at boot time
1 Create systemd configuration file
In the /etc/systemd/system/ directory, create a service configuration file, taking qt_display.service as an example
# 在 /etc/systemd/system/ 目录下,创建服务配置文件
sudo vim /etc/systemd/system/qt_display.service
Configuration 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.target
Meaning of each parameter in the configuration file
[Unit]
Description=Service Description
After=The service starts after which target or service starts
Requires=This service requires a target or service to be active before it can start.
Wants=The service would like a target or service to be active, but will not force it.
[Service]
Type=Service Type
ExecStartPre=Commands and scripts executed before the main process starts
ExecStart=Command to execute when starting the service --- main process
User=The user identity that runs the service
Environment=Set environment variables
Restart=Restart service request
RestartSec=Restart delay time
[Install]
WantedBy=Specifies when the target starts the service
2 Start system services
Reload system services, enable and start services
# 重新加载系统服务
sudo systemctl daemon-reload
# 启用服务
sudo systemctl enable qt_display.service
#启动服务
sudo systemctl start qt_display.service
Check whether the service starts normally. If it is normal, restart the system and verify whether the service starts automatically when the computer is turned on. If there is a problem, execute the following command to query the error
# 检查服务状态
sudo systemctl status qt_display.service
# 查看系统日志
journalctl -u qt_display.service
3 Other common commands
# 禁用系统服务
sudo systemctl disable qt_display.service
#停止服务
sudo systemctl stop qt_display.service