写个 nohup & 就跑,进程一挂就没人管。systemd 服务单元能自动拉起、记日志、管权限。

操作步骤

# /etc/systemd/system/myapp.service
[Unit]
Description=My App
After=network.target

[Service]
ExecStart=/usr/bin/python3 /opt/myapp/app.py
Restart=on-failure
User=myapp

[Install]
WantedBy=multi-user.target

要点速览

  • Restart=on-failure 让异常退出的进程自动复活,比 nohup 稳得多。
  • User 指定非 root 运行,缩小被攻破后的影响面。
  • systemctl enable 后开机自启,不用再写 rc.local。
后台进程交给 systemd,等于给它配了个永不疲倦的看护。