1. linux 下qt 应用开机自启,需要sudo时
文章目录
- 1. 创建 systemd 服务文件
- 2. 启用并启动服务
- 3. 检查服务状态
- 4. 为什么使用服务方式
1. 创建 systemd 服务文件
sudo vim /etc/systemd/system/aaa.service
名字随意,和应用相关即可,便于管理,后缀名为service
内容如下:
[Unit]
Description=XPP Qt Application
After=graphical.target
Wants=graphical.target[Service]
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/orangepi/.Xauthority
WorkingDirectory=/home/orangepi/ws-aaa/build
ExecStart=/home/orangepi/ws-aaa/build/aaa_rknn
User=orangepi
# 如果需要以 root 权限运行,移除上面的 User=orangepi 行
Restart=on-failure
RestartSec=5[Install]
WantedBy=graphical.target
2. 启用并启动服务
sudo systemctl daemon-reload
sudo systemctl enable aaa.service
sudo systemctl start aaa.service
3. 检查服务状态
sudo systemctl status aaa.service
4. 为什么使用服务方式
桌面端默认不是root用户,使用sudo管理员权限时无法使用desktop设置启动, 配置了 After=graphical.target 和 Wants=graphical.target
确保了服务会在图形界面完全启动后才运行,不会干扰桌面系统的启动过程。并且服务自启减少写守护进程的麻烦。