在 Debian 中,设置进程开机启动的方式取决于你使用的 init 系统版本(现在绝大多数 Debian 都用 systemd)。下面按常见情况说明。
假设你要开机启动 /opt/myapp/run.sh
sudo nano /etc/systemd/system/myapp.service
内容示例:
[Unit]
Description=My App Service
After=network.target
[Service]
ExecStart=/opt/myapp/run.sh
WorkingDirectory=/opt/myapp
Restart=on-failure
User=myuser
[Install]
WantedBy=multi-user.target
说明:
ExecStart:要执行的命令或脚本User:以哪个用户运行Restart:进程挂了是否重启sudo systemctl daemon-reload
sudo systemctl enable myapp.service
sudo systemctl start myapp
sudo systemctl status myapp
✅ 重启后就会自动启动
适用于快速测试,Debian 新版本默认可能没有 rc.local
sudo nano /etc/rc.local
示例:
#!/bin/bash
/opt/myapp/run.sh &
exit 0
sudo chmod +x /etc/rc.local
⚠️ 如果系统没有 rc-local.service,需要手动创建 systemd 支持
crontab -e
添加:
@reboot /opt/myapp/run.sh
✅ 重启后执行
❌ 不适合需要守护的进程
sudo update-rc.d myapp defaults
| 方式 | 推荐度 | 适用场景 |
|---|---|---|
| systemd | ⭐⭐⭐⭐⭐ | 正式服务 |
| crontab | ⭐⭐⭐ | 简单脚本 |
| rc.local | ⭐⭐ | 临时测试 |
如果你愿意,可以告诉我:
我可以帮你写完整可直接用的配置文件。