Debian 系統實現程序開機自啟動有多種方式,從簡單的 rc.local 到標準的 systemd 服務單元,再到傳統的 SysV init 腳本配合 insserv。本文系統梳理三大方案,提供標準化模板與最佳實踐,適用於 Debian 9/10/11/12 及衍生版。
# Debian 9+ 默認 rc-local 服務已存在但未啟用
systemctl enable --now rc-local
# 驗證服務狀態
systemctl status rc-local
#!/bin/bash
# /etc/rc.local
# 此腳本在多用戶運行級別結束時執行
# 示例:啟動 Shadowsocks 客戶端
/usr/local/bin/sslocal -c /etc/shadowsocks.json &
# 示例:啟動自定義服務
/home/user/start_myapp.sh &
exit 0
chmod +x /etc/rc.local
systemctl restart rc-local
限制:rc.local 在啟動晚期執行,無依賴管理、無日誌聚合、無重啟策略,**不適合關鍵生產服務**。
[Unit]
Description=My Application Service
Documentation=https://myapp.example.com/docs
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=myapp
Group=myapp
WorkingDirectory=/opt/myapp
Environment=CONFIG_FILE=/etc/myapp/config.yaml
EnvironmentFile=-/etc/default/myapp
ExecStart=/usr/local/bin/myapp --config ${CONFIG_FILE}
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5
StartLimitIntervalSec=60
StartLimitBurst=3
LimitNOFILE=65535
LimitNPROC=65535
# 安全加固
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/var/lib/myapp /var/log/myapp
[Install]
WantedBy=multi-user.target
# 重載配置
systemctl daemon-reload
# 啟用並啟動
systemctl enable --now myapp
# 常用操作
systemctl start|stop|restart|reload|status myapp
journalctl -u myapp -f # 實時日誌
systemctl cat myapp # 查看完整配置
#!/bin/sh
### BEGIN INIT INFO
# Provides: myapp
# Required-Start: $network $local_fs $remote_fs $syslog
# Required-Stop: $network $local_fs $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: My Application
# Description: Long description of my application
### END INIT INFO
NAME=myapp
DAEMON=/usr/local/bin/$NAME
USER=myapp
PIDFILE=/var/run/$NAME.pid
CONFIG=/etc/myapp/config.yaml
ARGS="--config=$CONFIG"
# 載入 LSB 函數庫
. /lib/lsb/init-functions
start() {
log_daemon_msg "Starting $NAME" "$NAME"
start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE \
--chuid $USER --exec $DAEMON -- $ARGS
log_end_msg $?
}
stop() {
log_daemon_msg "Stopping $NAME" "$NAME"
start-stop-daemon --stop --quiet --pidfile $PIDFILE
log_end_msg $?
}
case "$1" in
start) start ;;
stop) stop ;;
restart|reload|force-reload)
stop
start
;;
status)
status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload|force-reload|status}"
exit 2
;;
esac
exit 0
# 1. 賦予執行權限
chmod +x /etc/init.d/myapp
# 2. 測試腳本
/etc/init.d/myapp start
/etc/init.d/myapp status
/etc/init.d/myapp stop
# 3. 註冊開機自啟(insserv)
insserv /etc/init.d/myapp
# 4. 移除自啟
insserv -r /etc/init.d/myapp
# 5. 查看運行級別鏈接
ls -l /etc/rc*.d/ | grep myapp
| 參數 | 功能 |
|---|---|
-S, --start | 啟動服務 |
-K, --stop | 停止服務 |
-q, --quiet | 靜默模式 |
-b, --background | 後台運行 |
-m, --make-pidfile | 創建 PID 文件 |
-p, --pidfile | 指定 PID 文件路徑 |
-c, --chuid | 指定運行用戶 |
-a, --startas | 指定可執行文件路徑 |
-R, --retry | 重試超時/計劃 |
| 維度 | rc.local | systemd 服務 | SysV + insserv |
|---|---|---|---|
| 適用場景 | 臨時命令、簡單腳本、測試 | 所有生產服務 | 舊應用兼容、不支援 systemd 環境 |
| 依賴管理 | 無 | 完整(After/Before/Wants/Requires) | 基於 LSB 標頭 |
| 進程監管 | 無 | Restart=on-failure 等豐富策略 | 需腳本自行實現 |
| 日誌整合 | 無(標準輸出丟失) | journalctl 統一管理 | 自行重定向 |
| 資源限制 | 無 | LimitNOFILE, LimitNPROC, MemoryLimit 等 | 無 |
| 安全沙箱 | 無 | PrivateTmp, ProtectSystem, CapabilityBoundingSet | 無 |
journalctl -u myapp -xe 查看詳細錯誤User= 用戶是否存在、執行文件權限、工作目錄權限Environment= 或 EnvironmentFile=,不依賴 Shell 配置### BEGIN INIT INFO 區塊Debian 現代系統**首選 systemd 服務單元**,標準化、可觀測、可管理。rc.local 僅作臨時補丁,insserv 僅維護存量遺留系統。新建服務請直接編寫 .service 文件,享受系統級進程管理紅利。