CentOS 开机启动

来源:互联网 发布:李明老师的linux视频 编辑:程序博客网 时间:2024/06/16 05:08

欢迎访问我的个人博客网站:http://www.yanmin99.com/

一、开机启动介绍

  • 在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd、mysqld、postfix等,安装后系统默认不会自动启动的。就算手动执行 /etc/init.d/mysqld start 启动了服务,只要服务器重启后,系统仍然不会自动启动服务。

二、CentOS设置自动启动方式

1、CentOS7之前

  • A、利用 chkconfig 来配置启动级别

    • chkconfig介绍

      chkconfig –-add xxx //把服务添加到chkconfig列表chkconfig --del xxx //把服务从chkconfig列表中删除chkconfig xxx on //开启开机自动启动chkconfig xxx off //关闭开机自动启动chkconfig --list //查看所有chklist中服务chkconfig --list xxx 查看指定服务
    • chkconfig实例

      //把nginx添加到chkconfig中chkconfig --add nginx//查看nginx是否在chkconfig中(添加进去默认设置自动启动)chkconfig --list nginx//2~5都是on,就表明会自动启动了nginx 0:off 1:off   2:on    3:on    4:on    5:on    6:off//开启开机启动chkconfig nginx on//关闭开机启动chkconfig nginx offchkconfig --del nginx
  • B、修改 /etc/rc.d/rc.local

    /etc/rc.d/init.d/nginx start

CentOS7

  • 方案一

    • 赋予脚本可执行权限(/opt/script/autostart.sh是你的脚本路径)

      chmod +x /opt/script/autostart.sh
    • 打开/etc/rc.d/rc/local文件,在末尾增加如下内容

      /opt/script/autostart.sh
    • 在centos7中,/etc/rc.d/rc.local的权限被降低了,所以需要执行如下命令赋予其可执行权限

      chmod +x /etc/rc.d/rc.local
  • 方案二

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

      mv /opt/script/autostart.sh /etc/rc.d/init.d
    • 增加脚本的可执行权限

      chmod +x /etc/rc.d/init.d/autostart.sh
    • 添加脚本到开机自动启动项目中

      cd /etc/rc.d/init.dchkconfig --add autostart.shchkconfig autostart.sh on