在 Debian 中设置进程开机自启,常见有以下几种方式,按推荐程度从高到低说明:
以 /opt/myapp/run.sh 为例:
sudo nano /etc/systemd/system/myapp.service
内容示例:
[Unit]
Description=My App
After=network.target
[Service]
ExecStart=/opt/myapp/run.sh
WorkingDirectory=/opt/myapp
Restart=always
User=myuser
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp
systemctl status myapp
✅ 开机自动启动
✅ 崩溃可自动重启
编辑:
sudo nano /etc/rc.local
在 exit 0 前加:
/opt/myapp/run.sh &
确保有执行权限:
sudo chmod +x /etc/rc.local
⚠️ Debian 新版本默认可能没有 rc.local
crontab -e
添加:
@reboot /opt/myapp/run.sh
⚠️ 不适合复杂服务
⚠️ 无自动重启
sudo update-rc.d myapp defaults
如果你告诉我:
我可以帮你直接写完整配置。