Linux下(以RedHat爲範本)添加開機開機自動啓動腳本有兩種方式;vim
本例系統:Linux(CentOS 7.2)bash
使用 /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
一、授予 /etc/rc.d/rc.local 文件執行權限網絡
命令:chmod +x /etc/rc.d/rc.local函數
二、在文件文件底部添加腳本this
三、重啓服務器,查看腳本是否啓動spa
注意:/etc/rc.d/rc.local腳本執行,在/etc/profile以前,若/etc/rc.d/rc.local用到/etc/profile的環境變量,Shell沒法執行成功命令行
一、編輯服務腳本 xxxx(腳本名),增長內容(要在服務腳本中實現POSIX規範中的函數:start() stop()等),命令:vim /etc/init.d/xxxxcode
二、給腳本增長可執行權限,命令:chmod a+x /etc/init.d/xxxxblog
三、註冊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
四、列出當前的服務和他們的配置,命令:chkconfig --list
五、重啓服務器,查看腳本是否啓動