2022年2月7日 星期一

Ubuntu 20.04用systemd開啟rc.local

Ubuntu 20.04 server預設沒有/etc/rc.local。三步驟設定:

  1. 編輯/etc/systemd/system/rc-local.service
  2. 建立/etc/rc.local
  3. 設定開機啟動執行/etc/rc.local

步驟1: 編輯/etc/systemd/system/rc-local.service

sudo -i
cat > /etc/systemd/system/rc-local.service << _END_
[Unit]
 Description=/etc/rc.local Compatibility
 ConditionPathExists=/etc/rc.local

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

[Install]
 WantedBy=multi-user.target
_END_


步驟2:建立/etc/rc.local

printf '%s\n' '#!/bin/bash' 'exit 0' | sudo tee -a /etc/rc.local
sudo chmod +x /etc/rc.local


步驟3:設定開機啟動執行/etc/rc.local

sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

正常執行會看到類似下面的訊息

● rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset: enabled)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: active (exited) since Tue 2022-02-6 11:59:35 CST; 5s ago
    Process: 5056 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)

Feb 15 11:59:35 olallieberry systemd[1]: Starting /etc/rc.local Compatibility...
Feb 15 11:59:35 olallieberry systemd[1]: Started /etc/rc.local Compatibility.


參考資料

_EOF_

沒有留言:

張貼留言