Ubuntu 18.04 設置開機啟動腳本 rc.local systemd


ubuntu18.04不再使用initd管理系統,改用systemd

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

1.建立rc-local.service文件

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

2.復制以下內容

[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
1) [Unit] 區塊:啟動順序與依賴關系。

2) [Service] 區塊:啟動行為,如何啟動,啟動類型。

3) [Install] 區塊,定義如何安裝這個配置文件,即怎樣做到開機啟動。
有關說明

 

3. 創建我們自定義的啟動腳本(以后自己的腳本在這里面添加即可)

$ sudo vi /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.
touch /home/xxx/`date +%Y-%m-%d`.log 
echo "`df`" >  /home/xxx/`date +%Y-%m-%d`.log  
echo "看到這行字,說明添加自啟動腳本成功。" > /usr/local/test.log
exit 0
#!/bin/sh -e

-e 參數可以讓腳本執行遇到錯誤就不往下執行
有關說明

 

 

$ sudo chmod +x /etc/rc.local && sudo systemctl enable rc-local
Created symlink /etc/systemd/system/multi-user.target.wants/rc-local.service → /etc/systemd/system/rc-local.service.

 

 

啟動服務,並檢查服務(非必要)

$ sudo systemctl start rc-local.service
$ sudo systemctl status rc-local.service

 rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset:
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: active (exited) since Fri 2019-01-25 09:31:07 CST; 9s ago
  Process: 17588 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)

Jan 25 09:31:07 SCHIPS systemd[1]: Starting /etc/rc.local Compatibility...
Jan 25 09:31:07 SCHIPS systemd[1]: Started /etc/rc.local Compatibility.




參考:

https://www.jianshu.com/p/79d24b4af4e5

https://www.cnblogs.com/defifind/p/9285456.html

 


免責聲明!

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



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