0x00 前言
CentOS 6 和CentOS 7的開機啓動項有所區別,這裏分享下CentOS 7下添加開機啓動腳本的方法。
0x01 創建開機啓動腳本
touch /home/test/autorun.sh
編輯並查看autorun.sh腳本
#/bin/bash
service docker start
0x02 如何加入開機啓動項
方法一 在/etc/rc.d/rc.local中加入運行命令(推薦)
-
先看下/etc/rc.d/rc.local的說明
$ cat /etc/rc.local #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local
rc.local 最後兩行註釋是使用方法,大意是:
必須給/etc/rc.d/rc.local增加執行權限,已保證這個腳本能在開機啓動時被執行。
-
把運行自定義腳本的命令追加到rc.local文件中,如下
#!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local bash /home/test/autorun.sh;
-
爲/etc/rc.d/rc.local增加執行權限
chmod +x /etc/rc.d/rc.local
方法二 通過chkconfig控制
chkconfig控制的方法比較適合服務,因爲可以通過chkconfig控制開啓和關閉,比較靈活。
-
按照chkconfig格式書寫自定義腳本
#/bin/bash #chkconfig:23 90 10 #decription:some desc for this script service docker start
這裏需要說的是chkconfig:後面的3列分別表示啓動等級,啓動優先級和關閉優先級
優先級的範圍好理解,範圍是1-100,值越小,優先級越搞
-
將腳本移動到/etc/rc.d/init.d目錄下
mv /home/test/autorun.sh /etc/rc.d/init.d
-
增加自定義腳本的可執行權限
chmod +x /etc/rc.d/init.d/autorun.sh
-
添加自定義腳本到開機自動啓動項中,並開啓
cd /etc/rc.d/init.d chkconfig --add autorun.sh chkconfig autorun.sh on
0x03 參考文獻
https://www.cnblogs.com/qzqdy/p/9596100.html
https://www.cnblogs.com/longchengruoxi/p/11451062.html