【Linux】開機自動啟動腳本


  Linux下(以RedHat為范本)添加開機開機自動啟動腳本有兩種方式;

  本例系統:Linux(CentOS 7.2)

方法一

  使用 /etc/rc.d/rc.local,自動啟動腳本

 1 #!/bin/bash                                                                                                
 2 # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES                                                            
 3 #                                                                                                          
 4 # It is highly advisable to create own systemd services or udev rules                                      
 5 # to run scripts during boot instead of using this file.                                                   
 6 #                                                                                                          
 7 # In contrast to previous versions due to parallel execution during boot                                   
 8 # this script will NOT be run after all other services.                                                    
 9 #                                                                                                          
10 # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure                                    
11 # that this script will be executed during boot.                                                           
12                                                                                                            
13 touch /var/lock/subsys/local  

  1、授予 /etc/rc.d/rc.local 文件執行權限

    命令:chmod +x /etc/rc.d/rc.local

  2、在文件文件底部添加腳本

  3、重啟服務器,查看腳本是否啟動

  注意:/etc/rc.d/rc.local腳本執行,在/etc/profile之前,若/etc/rc.d/rc.local用到/etc/profile的環境變量,Shell無法執行成功

方法二

  1、編輯服務腳本 xxxx(腳本名),增加內容(要在服務腳本中實現POSIX規范中的函數:start() stop()等),命令:vim /etc/init.d/xxxx

  2、給腳本增加可執行權限,命令:chmod a+x /etc/init.d/xxxx

  3、注冊xxxx服務名,命令:chkconfig --add xxxx

 1 注意執行命令:chkconfig --add xxxx
 2 常常會出現
 3 
 4 service myservice does not support chkconfig
 5 我們一般在腳本開頭加入下面兩句就好了
 6 #vim /etc/init.d/xxxx
 7 添加下面兩句到 #!/bin/bash 之后。
 8 
 9 # chkconfig: 2345 10 90 
10 # description: xxxx ....
11 其中2345是默認啟動級別,級別有0-6共7個級別。
12 
13   等級0表示:表示關機   
14 
15   等級1表示:單用戶模式   
16 
17   等級2表示:無網絡連接的多用戶命令行模式   
18 
19   等級3表示:有網絡連接的多用戶命令行模式   
20 
21   等級4表示:不可用   
22 
23   等級5表示:帶圖形界面的多用戶模式   
24 
25   等級6表示:重新啟動
26 
27 10是啟動優先級,90是停止優先級,優先級范圍是0-100,數字越大,優先級越低

    配置系統啟動時該腳本默認啟動,命令:chkconfig xxxx on   

    配置系統啟動時該腳本默認關閉,命令:chkconfig xxxx off

  4、列出當前的服務和他們的配置,命令:chkconfig --list

  5、重啟服務器,查看腳本是否啟動


免責聲明!

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



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