ubuntu18.04開機自啟動腳本(已測試可以)


 

 

 

 

腳本設置流程及解析過程:

一:腳本設置流程:

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

1、建立rc-local.service文件

1

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

2、將下列內容復制進rc-local.service文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

[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

3、創建文件rc.local  

1

sudo vi /etc/rc.local

4、將下列內容復制進rc.local文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#!/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 "看到這行字,說明添加自啟動腳本成功。" /usr/local/test.log

exit 0

5、給rc.local加上權限

1

sudo chmod +x /etc/rc.local

6、啟用服務

1

sudo systemctl enable rc-local

7、啟動服務並檢查狀態

1

2

sudo systemctl start rc-local.service

sudo systemctl status rc-local.service

 

 

 

 

8、重啟並檢查test.log文件

1

cat /usr/local/test.log  

 

如果能看到內容,說明設置成功,你就可以通過編輯rc.local文件來設置啟動腳本了

二:解析說明:

參閱下列鏈接

https://askubuntu.com/questions/886620/how-can-i-execute-command-on-startup-rc-local-alternative-on-ubuntu-16-10

 關於 ubuntu-18.04 LTS 版本是在 ubuntu 的官方論壇看到原來 ubuntu-16.10 開始不再使用initd管理系統,改用systemd…

systemd is now used for user sessions. System sessions had already been provided by systemd in previous Ubuntu releases.

快速看了 systemd 的使用方法,發現改動有點大, 包括用 systemctl 命令來替換了 service 和 chkconfig 的功能。

比如以前啟動 mysql 服務用:

sudo service mysql start
 

現在用:

sudo systemctl start mysqld.service
 

其實這個改動到不是算大,主要是開機啟動比以前復雜多了。systemd 默認讀取 /etc/systemd/system 下的配置文件,該目錄下的文件會鏈接/lib/systemd/system/下的文件。

執行 ls /lib/systemd/system 你可以看到有很多啟動腳本,其中就有我們需要的 rc.local.service

打開腳本內容:

  1.  
    # This file is part of systemd.
  2.  
    #
  3.  
    # systemd is free software; you can redistribute it and/ or modify it
  4.  
    # under the terms of the GNU Lesser General Public License as published by
  5.  
    # the Free Software Foundation; either version 2.1 of the License, or
  6.  
    # (at your option) any later version.
  7.  
     
  8.  
    # This unit gets pulled automatically into multi-user.target by
  9.  
    # systemd-rc- local-generator if /etc/rc. local is executable.
  10.  
    [ Unit]
  11.  
    Description=/etc/rc. local Compatibility
  12.  
    ConditionFileIsExecutable=/etc/rc. local
  13.  
    After=network.target
  14.  
     
  15.  
    [Service]
  16.  
    Type=forking
  17.  
    ExecStart=/etc/rc. local start
  18.  
    TimeoutSec= 0
  19.  
    RemainAfterExit=yes

一般正常的啟動文件主要分成三部分

[Unit] 段: 啟動順序與依賴關系 
[Service] 段: 啟動行為,如何啟動,啟動類型 
[Install] 段: 定義如何安裝這個配置文件,即怎樣做到開機啟動

可以看出,/etc/rc.local 的啟動順序是在網絡后面,但是顯然它少了 Install 段,也就沒有定義如何做到開機啟動,所以顯然這樣配置是無效的。 因此我們就需要在后面幫他加上 [Install] 段:

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

這里需要注意一下,ubuntu-18.04 默認是沒有 /etc/rc.local 這個文件的,需要自己創建

sudo touch /etc/rc.local
 

然后把你需要啟動腳本寫入 /etc/rc.local ,我們不妨寫一些測試的腳本放在里面,以便驗證腳本是否生效.

echo "this just a test" > /usr/local/text.log
 

做完這一步,還需要最后一步 前面我們說 systemd 默認讀取 /etc/systemd/system 下的配置文件, 所以還需要在 /etc/systemd/system 目錄下創建軟鏈接

ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
 

OK, 接下來,重啟系統,然后看看 /usr/local/text.log 文件是否存在就知道開機腳本是否生效了。

備注:文章轉自他人,僅供學習交流使用。


免責聲明!

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



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