ubuntu18.04設(shè)置每次開機自啟動后執(zhí)行臨時掛載命令

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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容