萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Debian 系統設置開機自啟動完整指南:rc.local、systemd 服務與 insserv 管理

Debian 系統實現程序開機自啟動有多種方式,從簡單的 rc.local 到標準的 systemd 服務單元,再到傳統的 SysV init 腳本配合 insserv。本文系統梳理三大方案,提供標準化模板與最佳實踐,適用於 Debian 9/10/11/12 及衍生版。

方案一:rc.local(最簡單、適合臨時/單命令)

啟用 rc.local 服務


# Debian 9+ 默認 rc-local 服務已存在但未啟用
systemctl enable --now rc-local

# 驗證服務狀態
systemctl status rc-local

編輯 /etc/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 在啟動晚期執行,無依賴管理、無日誌聚合、無重啟策略,**不適合關鍵生產服務**。

方案二:systemd 服務單元(現代標準、推薦生產環境)

創建服務文件:/etc/systemd/system/myapp.service


[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         # 查看完整配置

方案三:SysV init 腳本 + insserv(傳統兼容、老項目維護)

創建初始化腳本:/etc/init.d/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

start-stop-daemon 關鍵參數速查

參數功能
-S, --start啟動服務
-K, --stop停止服務
-q, --quiet靜默模式
-b, --background後台運行
-m, --make-pidfile創建 PID 文件
-p, --pidfile指定 PID 文件路徑
-c, --chuid指定運行用戶
-a, --startas指定可執行文件路徑
-R, --retry重試超時/計劃

三大方案對比與選型建議

維度rc.localsystemd 服務SysV + insserv
適用場景臨時命令、簡單腳本、測試所有生產服務舊應用兼容、不支援 systemd 環境
依賴管理無完整(After/Before/Wants/Requires)基於 LSB 標頭
進程監管無Restart=on-failure 等豐富策略需腳本自行實現
日誌整合無(標準輸出丟失)journalctl 統一管理自行重定向
資源限制無LimitNOFILE, LimitNPROC, MemoryLimit 等無
安全沙箱無PrivateTmp, ProtectSystem, CapabilityBoundingSet無

常見問題排查

  • systemd 服務啟動失敗:journalctl -u myapp -xe 查看詳細錯誤
  • 權限被拒:檢查 User= 用戶是否存在、執行文件權限、工作目錄權限
  • 環境變量不生效:使用 Environment= 或 EnvironmentFile=,不依賴 Shell 配置
  • insserv 報錯 "missing LSB tags":腳本缺少 ### BEGIN INIT INFO 區塊

Debian 現代系統**首選 systemd 服務單元**,標準化、可觀測、可管理。rc.local 僅作臨時補丁,insserv 僅維護存量遺留系統。新建服務請直接編寫 .service 文件,享受系統級進程管理紅利。

copyright © 萬盛學電腦網 all rights reserved