在linux開機(jī)自啟動(dòng)中通秤抗ィ可以添加腳本在rc.local中,所以需要自己生產(chǎn)并注冊(cè)
1.Ubuntu18.04生成rc-local.service
sudo gedit /etc/systemd/system/rc-local.service
將以下內(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 #配置的腳本文件rc.local為start
TimeoutSec=0
StandardOutput=tty #標(biāo)準(zhǔn)輸出
RemainAfterExit=yes
SysVStartPriority=99 #優(yōu)先級(jí)围详,當(dāng)有多個(gè)開機(jī)啟動(dòng)文件時(shí)可以設(shè)置不同的值
[Install]
WantedBy=multi-user.target
2.生成rc.local
sudo gedit /etc/rc.local
將一下內(nèi)容復(fù)制進(jìn)文件
#!/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 "看到這行字泼舱,說(shuō)明添加自啟動(dòng)腳本成功。" > /usr/local/test.log
exit 0
3.給rc.local加上權(quán)限,啟用服務(wù),啟動(dòng)服務(wù)并檢查狀態(tài)
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
4.利用rc.local文件配置開機(jī)自啟動(dòng)腳本
sudo gedit /etc/rc.local
打開腳本文件,然后在exit 0前面加入需要執(zhí)行的代碼即可容客。
然后再執(zhí)行下面代碼(18.04需要,其他系統(tǒng)可能不需要约郁,可以不執(zhí)行缩挑,重啟試一下)
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service