CentOS7下添加开机启动脚本

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中加入运行命令(推荐)

  1. 先看下/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增加执行权限,已保证这个脚本能在开机启动时被执行。

  2. 把运行自定义脚本的命令追加到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;
    
  3. 为/etc/rc.d/rc.local增加执行权限

    chmod +x /etc/rc.d/rc.local
    

方法二 通过chkconfig控制

chkconfig控制的方法比较适合服务,因为可以通过chkconfig控制开启和关闭,比较灵活。

  1. 按照chkconfig格式书写自定义脚本

    #/bin/bash
    #chkconfig:23 90 10
    #decription:some desc for this script
    service docker start
    

    这里需要说的是chkconfig:后面的3列分别表示启动等级,启动优先级和关闭优先级

    优先级的范围好理解,范围是1-100,值越小,优先级越搞

  2. 将脚本移动到/etc/rc.d/init.d目录下

    mv  /home/test/autorun.sh /etc/rc.d/init.d
    
  3. 增加自定义脚本的可执行权限

    chmod +x  /etc/rc.d/init.d/autorun.sh
    
  4. 添加自定义脚本到开机自动启动项中,并开启

    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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值