Ubuntu 18.04 rc.local systemd設置


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

然而systemd很難用,改變太大,跟之前的完全不同。

使用systemd設置開機啟動
為了像以前一樣,在/etc/rc.local中設置開機啟動程序,需要以下幾步:

1、systemd默認讀取/etc/systemd/system下的配置文件,該目錄下的文件會鏈接/lib/systemd/system/下的文件。一般系統安裝完/lib/systemd/system/下會有rc-local.service文件,即我們需要的配置文件。
鏈接過來:

ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service  
cd /etc/systemd/system/  
cat rc-local.service  

  

rc-local.service內容

#  SPDX-License-Identifier: LGPL-2.1+  
#  
#  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 Compatibility  
Documentation=man:systemd-rc-local-generator(8)  
ConditionFileIsExecutable=/etc/rc.local  
After=network.target  

[Service]  
Type=forking  
ExecStart=/etc/rc.local start  
TimeoutSec=0  
RemainAfterExit=yes  
GuessMainPID=no  

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

  

1) [Unit] 區塊:啟動順序與依賴關系。

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

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

2、創建/etc/rc.local文件

touch /etc/rc.local 

  

3、賦可執行權限

chmod 755 /etc/rc.local  

  

4、編輯rc.local,添加需要開機啟動的任務

#!/bin/bash  

echo "test rc " > /var/test.log 

  

5、執行reboot重啟系統,然后查看test.log


免責聲明!

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



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