在Ubuntu 18.04系統不能像其它Ubuntu一樣采用編輯rc.local文件來設置開機啟動腳本,你可以通過下面的步驟設置后使rc.local文件可用。請按以下步驟操作。
1、建立rc-local.service文件
運行命令:
sudo vi /etc/systemd/system/rc-local.service
2、將下面代碼復制到rc-local.service文件中
在上面建議rc-local.service文件后復制下面代碼到該文件中:
[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
3、創建文件rc.local
運行命令:
sudo vi /etc/rc.local
4、將下面代碼復制到rc.local文件
把下面代碼復制到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.
echo "看到這行字,說明添加自啟動腳本成功。" > /usr/local/test.log
exit 0
5、給rc.local加上權限
運行命令:
sudo chmod +x /etc/rc.local
6、開啟服務
運行命令:
sudo systemctl enable rc-local
7、啟動服務并檢查狀態
直接運行以下兩個命令:
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
8、重啟并檢查test.log文件
做完以上七個步驟就重新啟動系統,運行以下命令檢查test.log文件:
cat /usr/local/test.log
結語
如果能看到test.log文件的內容,就說明上面的設置成功了。現在可以通過編輯rc.local文件的方法來設置啟動腳本了。