Ubuntu18.04生成rc.local
Ubuntu沒有自帶其他linux版本自帶的rc.local文件的開機自啟動方法,所以要自己生成rc.local文件:
生成rc-local.service文件將一下內容複制進rc-local…service文件 sudo gedit /etc/systemd/system/rc-local.service
將一下內容複制進該文件[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local #腳本文件位置
[Service]
Type=forking
ExecStart=/etc/rc.local start #配置的腳本文件rc.local為start
TimeoutSec=0
StandardOutput=tty #標准輸出
RemainAfterExit=yes
SysVStartPriority=99 #優先級,當有多個開機啟動文件時可以設置不同的值
[Install]
WantedBy=multi-user.target
生成rc.local sudo gedit /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.
echo "看到這行字,說明添加自啟動腳本成功。" > /usr/local/test.log
exit 0
給rc.local加上權限,啟用服務,啟動服務並檢查狀態sudo chmod +x /etc/rc.local
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
重啟並檢查test.log文件 cat /usr/local/test.log
利用rc.local文件配置開機自啟動腳本
sudo gedit /etc/rc.local
打開腳本文件,然後在exit 0前面加入需要執行的代碼即可。 然後再執行下面代碼(18.04需要,其他系統可能不需要,可以不執行,重啟試一下)
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
參考鏈接 ubuntu-18.04 設置開機啟動腳本