ubuntu-18.04 ubuntu-16.04 設置開機啓動腳本

ubuntu-18.04 設置開機啓動

ubuntu-16.10 開始不再使用initd管理系統,改用systemd

執行 ls /lib/systemd/system 你可以看到有很多啓動腳本,其中就有我們需要的 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 

文件內容

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

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

對於Unit、Service配置不用管,我們如果自己配置開機自啓動服務時候,需要配置Install配置。

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

創建rc.local並授權

ubuntu-18.04 默認是沒有 /etc/rc.local 這個文件的,需要自己創建,並且給該文件賦予可執行權限(否則文件不能執行,即開機不能運行,即開機自動啓動會不成功

sudo touch /etc/rc.local
chmod 755 /etc/rc.local

編輯rc.local 並測試

添加需要開機啓動的任務(這裏需要注意,一定要有#!/bin/bash,即指定解釋此腳本的解釋器shell的路徑,否則未指定解釋器會執行不成功

#!/bin/bash  
echo "test rc " > /usr/test.log  

重啓服務器reboot,此事後就會看到/var 目錄下面就會有生成的test.log,說明服務自啓動成功了,rc.local裏面的命令得到執行

添加自己的服務,放到rc.local

#!/bin/bash                                                                                                                  
                                                                                                                             
/opt/zbox/zbox start                                                                                                         
                                                                                                                             
emqx start                                                                                                                   
                                                                                                                             
redis-server 

現在重啓reboot,自己的服務就會子機器啓動時候自動啓動服務了。

參考

ubuntu-16.04 設置開機啓動

ubuntu-16.04設置開機啓動問題,還是蠻常規操作的,但是也會出現一些問題,比如設置無效等坑。

Ubuntu 16.04 設置 程序開機啓動 用/etc/ rc.local開機啓動命令/腳本的方法

網上找了好多方法都不好使,其中有幾個點需要注意下就行了,這裏強調幾個重要步驟:

  • 用最簡單方法,就在/etc/rc.local 文件中添加開機啓動命令
  • /etc/rc.local 文件中的第一行 #!/bin/sh 更改爲 #!/bin/bash
  • 配置程序啓動命令,也配置一個測試的命令,看rc.local 是否開機啓動執行命令了。
sudo /opt/zbox/zbox start

sudo /opt/emqx/bin/emqx start

echo "#define MAIN_H" >> /opt/main111.h

其中,echo “#define MAIN_H” >> /opt/main111.h 如果ubuntu重啓後,/opt/main111.h 文件下有了字符串,說明rc.local 開機時執行了的。

  • 注意一個點,執行命令的時候,是需要系統權限的,命令前面一定要加sudo,實測如果不加sudo,怎麼都啓動不了命令。這裏是個坑。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章