本文硬件平臺以飛凌嵌入式OK3399-C開發板為基礎進行講解,其它RK3399產品,由于各個廠家設置不同會有所差異,請參考使用。本文檔主要介紹飛凌RK3399開發板Desktop版本如何設置開機自啟服務。
systemd 默認讀取 /etc/systemd/system 下的配置文件,該目錄下的文件會鏈接/lib/systemd/system/下的文件。 執行 ls /lib/systemd/system 你可以看到有很多啟動腳本,其中就有我們需要的 rc.local.service 打開腳本內容(如果沒有就創建):
# This file is part of systemd.## systemd is free software; you can redistribute it and/or modify it# under the terms of the GNU Lesser General Public License as published by# the Free Software Foundation; either version 2.1 of the License, or# (at your option) any later version.# This unit gets pulled automatically into multi-user.target by# systemd-rc-local-generator if /etc/rc.local is executable.[Unit]Description=/etc/rc.local CompatibilityConditionFileI>**ecutable=/etc/rc.localAfter=network.target[Service][color=inherit !important]Type=forkingExecStart=/etc/rc.local startTimeoutSec=0RemainAfterExit=yes
一般正常的啟動文件主要分成三部分 [Unit] 段: 啟動順序與依賴關系
[Service] 段: 啟動行為,如何啟動,啟動類型
[Install] 段: 定義如何安裝這個配置文件,即怎樣做到開機啟動 可以看出,/etc/rc.local 的啟動順序是在網絡后面,但是顯然它少了 Install 段,也就沒有定義如何做到開機啟動,所以顯然這樣配置是無效的。 因此我們就需要在后面幫他加上 [Install] 段:
[Install] WantedBy=multi-user.target Alias=rc-local.service
所以完整的 rc.local.service 文件是這樣的:
[Unit]Description=/etc/rc.local CompatibilityDocumentation=man:systemd-rc-local-generator(8)ConditionFileI>**ecutable=/etc/rc.localAfter=network.target[Service]Type=forkingExecStart=/etc/rc.local startTimeoutSec=0RemainAfterExit=yesGuessMainPID=no[Install]WantedBy=multi-user.targetAlias=rc-local.service
這里需要注意一下,ubuntu-18.04 server 版默認是沒有 /etc/rc.local 這個文件的,需要自己創建
sudo touch /etc/rc.local
然后把你需要啟動腳本寫入 /etc/rc.local ,我們不妨寫一些測試的腳本放在里面,以便驗證腳本是否生效.
echo "this just a test" > /usr/local/text.log
記得給加上執行權限:
sudo chmod +x /etc/rc.local
做完這一步,還需要最后一步 前面我們說 systemd 默認讀取 /etc/systemd/system 下的配置文件, 所以還需要在 /etc/systemd/system 目錄下創建軟鏈接
ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
接下來,重啟系統,然后看看/usr/local/text.log 文件是否存在就知道開機腳本是否生效了。 https://www.forlinx.com/article_view_593.html
|