Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Debian 系統設置程序開機自啟動:systemd、rc.local、SysVinit 三種方式

編輯:關於 Linux

在 Debian 系統中讓程序/腳本開機自動運行,現代標準是 systemd 服務單元,兼容舊有 /etc/rc.local 與 SysVinit /etc/init.d/ 腳本。本文詳細演示三種方式的配置、優缺點、調試技巧、常見坑點。

systemd 服務單元示例

方式一:systemd 服務單元(推薦,Debian 8+ 默認)

1. 創建服務文件


# /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target
Wants=network-online.target

[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/opt/myapp
ExecStart=/usr/local/bin/myapp --config /etc/myapp/config.yaml
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
# 環境變量
Environment=APP_ENV=production
#EnvironmentFile=/etc/myapp/env

[Install]
WantedBy=multi-user.target

2. 重載、啟用、啟動


systemctl daemon-reload
systemctl enable myapp.service   # 開機自啟
systemctl start myapp.service    # 立即啟動
systemctl status myapp.service   # 查看狀態
journalctl -u myapp.service -f   # 實時日志

關鍵 Type 選項

Type適用場景特點
simple前台運行程序(默認)systemd 認為啟動即服務就緒
forking傳統雙重 fork 守護進程需 PIDFile,父進程退出後子進程為主進程
oneshot執行一次即退出的腳本配合 RemainAfterExit=yes 保持 active 狀態
notify程序調用 sd_notify() 通知就緒最精確,需程序支持
dbusD-Bus 激活服務較少用

方式二:/etc/rc.local(兼容舊習慣,簡單腳本適用)

1. 確保 rc-local 服務存在並啟用


# Debian 9+ 默認無 rc.local,需創建
cat > /etc/rc.local <<'EOF'
#!/bin/bash
# 這裡添加開機命令,需完整路徑
/opt/scripts/start_firewall.sh
/usr/local/bin/sslocal -c /etc/shadowsocks.json -d start
exit 0
EOF

chmod +x /etc/rc.local

# 創建 systemd 服務拉起 rc.local
cat > /etc/systemd/system/rc-local.service <<'EOF'
[Unit]
Description=/etc/rc.local Compatibility
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable rc-local.service
systemctl start rc-local.service

注意事項

  • 腳本必須可執行(chmod +x)、Shebang 指定解釋器(#!/bin/bash)。
  • 命令需完整路徑,環境變量極少(PATH=/sbin:/bin:/usr/sbin:/usr/bin)。
  • 長時間運行程序建議用 nohup cmd & 或 setsid,否則阻塞啟動流程。
  • 無日志管理、無依賴控制、無重啟策略,不適合生產核心服務。

方式三:SysVinit 腳本(/etc/init.d/,兼容極舊軟件)

標準 LSB 標頭腳本模板


#!/bin/bash
### BEGIN INIT INFO
# Provides:          aria2c
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Aria2 Download Manager
# Description:       Aria2 Download Manager
### END INIT INFO

NAME=aria2c
USER=pi
DAEMON=/usr/bin/$NAME
PIDFILE=/var/run/$NAME.pid
CONF=/home/$USER/.aria2/aria2.conf
ARGS="--conf-path=${CONF}"

. /lib/lsb/init-functions

case "$1" in
  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 $?
    ;;
  restart|reload|force-reload)
    log_daemon_msg "Restarting $NAME" "$NAME"
    start-stop-daemon --stop --quiet --retry 5 --pidfile $PIDFILE
    start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --chuid $USER --exec $DAEMON -- $ARGS
    log_end_msg $?
    ;;
  status)
    status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 2
    ;;
esac
exit 0

安裝與管理


chmod +x /etc/init.d/aria2c
update-rc.d aria2c defaults   # 生成 rc?.d 鏈接
service aria2c start
service aria2c status

三種方式對比選型

維度systemdrc.localSysVinit
推薦度⭐⭐⭐⭐⭐⭐⭐⭐
依賴管理完善無基礎
進程監管/重啟內建無手動實現
日志集中化journalctl無無
資源限制cgroups 完整支持無有限
調試便利性systemd-analyze, journalctl困難一般
適用場景所有生產服務簡單一次性腳本、測試極舊軟件、嵌入式

常見坑點與調試

現象原因解決
systemd 服務啟動即失敗ExecStart 路徑錯誤、權限不足、依賴服務未就緒systemctl status、journalctl -u、檢查 After=
rc.local 不執行rc-local.service 未啟用、腳本無執行權限、Shebang 缺失檢查 systemctl status rc-local、chmod +x
開機卡在「A start job is running」服務 Type=simple 但程序後台化、或阻塞等待改 Type=forking/notify、或程序前台運行
環境變量不生效systemd 不繼承 Shell 環境用 Environment= / EnvironmentFile= 顯式聲明

進階:用戶級 systemd 服務 (無需 root)


# ~/.config/systemd/user/myapp.service
[Unit]
Description=My User Service

[Service]
ExecStart=/home/user/.local/bin/myapp
Restart=on-failure

[Install]
WantedBy=default.target

# 啟用 (用戶登錄後生效,或啟用 linger 開機即啟動)
systemctl --user daemon-reload
systemctl --user enable --now myapp.service
loginctl enable-linger user   # 開機自啟用戶服務

選擇 systemd 服務單元作為首選,rc.local 臨時過渡,SysVinit 僅兼容舊系統。規範的服務化管理是 Linux 運維自動化、可觀測、高可用的基石。

Copyright © Windows教程網 All Rights Reserved