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