ubuntu-18.04不能像ubuntu14一樣通過編輯rc.local來設(shè)置開機啟動腳本,通過下列簡單設(shè)置后,可以使rc.local重新發(fā)揮作用。
1.建立rc-local.service文件
sudo vim /etc/systemd/system/rc-local.service
2.將下列內(nèi)容復(fù)制進(jìn)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.創(chuàng)建文件rc.local
sudo vim /etc/rc.local
4.將下列內(nèi)容復(fù)制進(jìn)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加上權(quán)限,啟用服務(wù)
加上權(quán)限
sudo chmod +x /etc/rc.local
啟用服務(wù)
sudo systemctl enable rc-local
6.啟動服務(wù)并檢查狀態(tài)
啟動服務(wù)
sudo systemctl start rc-local.service
檢查狀態(tài)
sudo systemctl status rc-local.service
7.重啟并檢查test.log文件
cat /usr/local/test.log
如果以上成功了,那么只需稍微改一下。
注意:我使用的方法是將/etc/rc.local的文件當(dāng)做一個啟動索引文件來使用,就是把它當(dāng)做一個專門用來啟動其他.sh文件來使用的,你也可以直接用/etc/rc.local來啟動你想要啟動的程序或者腳本。
1.創(chuàng)建你自己需要的 .sh文件
sudo vim test.sh
文件名:test.sh
shell腳本
#!\bin\bash
cd /home/lbw/
python ce.py
exit 0
然后加執(zhí)行權(quán)限
sudo chmod +x test.sh
如圖:

image.png
2.寫上你需要執(zhí)行的內(nèi)容
例如:前提是你寫好了py文件
sudo vim ce.py
文件名:ce.py
python測試小程序
with open("sb.txt", "w") as f:
f.write("SB")
3.修改一下剛才測試用的文件
sudo vim /etc/rc.local

image.png
啟動 test.sh 這個文件,當(dāng)然,你可以隨便定義名字與位置
注意:如果不成功,那么可以使用
sudo systemctl status rc-local.service