IT Knowledge Base

~ Freedom is the right of all sentient beings ~

發佈日期:

分類:

如何在Ubuntu 18.04‧設定開機腳本(startup script)

01. 以前直接建立/etc/rc.local檔案,就可以執行開機腳本。但來到Ubuntu 18.04,這方法已經行不通了。

02. 但我們可以建立一個特別服務,便可以繼續執行/etc/rc.local檔案。

03. 首先建立rc-local.service檔案。

sudo nano /etc/systemd/system/rc-local.service

04. 在檔案加入以下內容。

[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

05. 而在/etc/rc.local檔案,加入以下內容

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# ... code here ...

exit 0

06. 為/etc/rc.local檔案加入執行權限

sudo chmod +x /etc/rc.local

07. 啟用rc-local.service服務。

sudo systemctl enable rc-local

08. 啟動服務。

sudo systemctl start rc-local.service

09. 如需檢查服務狀態,可執行以下命令。

sudo systemctl status rc-local.service

10 重啟系統,測試rc.local內容是否已被執行。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *