Ubuntu/Debian systemd 服务自启动
需要作为后台服务运行的应用,建议创建 systemd 服务。
创建 /etc/systemd/system/myapp.service:
[Unit]
Description=My Application
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=firefly
WorkingDirectory=/home/firefly/myapp
ExecStart=/home/firefly/myapp/myapp
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target启用并启动服务:
sudo systemctl daemon-reload
sudo systemctl enable --now myapp.service
sudo systemctl status myapp.service查看日志:
journalctl -u myapp.service -f如果服务启动的是图形程序,需要补充显示环境和权限,例如:
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/firefly/.Xauthority
