Ubuntu18.04 設置開機自啟動服務


Ubuntu-18.04不能像ubuntu14一樣通過編輯rc.local來設置開機啟動腳本,通過下列簡單設置后,可以使rc.local重新發揮作用。

1、編輯rc-local.service文件

sudo vi /etc/systemd/system/rc-local.service

 

2、在rc-local.service文件中加入以下內容

[Install]  
WantedBy=multi-user.target Alias=rc-local.service

一般啟動文件需要三個組成部分

[Unit]段: 啟動順序與依賴關系

[Service] 段: 啟動行為,如何啟動,啟動類型

[Install] 段: 定義如何安裝這個配置文件,即怎樣做到開機啟動

3、創建文件rc.local ,Ubuntu-18.04 默認是沒有 /etc/rc.local 這個文件的,需要自己創建

sudo vi /etc/rc.local

4、將下列內容復制進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.
sudo echo "在這里行寫入你需自啟動服務的腳本" > /usr/local/text.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文件

cat /usr/local/test.log

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM