centos使用--开机启动
centos6.8
1.利用 chkconfig 来配置启动级别
在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd、mysqld、postfix等,安装后系统默认不会自动启动的。就算手动执行 /etc/init.d/mysqld start 启动了服务,只要服务器重启后,系统仍然不会自动启动服务。 在这个时候,我们就需要在安装后做个设置,让系统自动启动这些服务,避免不必要的损失和麻烦。 其实命令很简单的,使用chkconfig即可。
chkconfig mysqld on //设置为开机自动启动
chkconfig postfix off //取消掉某个服务自动启动
chkconfig –-add postfix //如果这个服务尚未被添加到 chkconfig 列表中
chkconfig --list //查询当前所有自动启动的服务
chkconfig --list httpd //查看指定的服务
/*
|-----------------------------------
| 当我们输入
| chkconfig --list httpd
| 1httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
| 此时0~6均为off,则说明httpd服务不会在系统启动的时候自动启动。我们输入:
| chkconfig httpd on
| 1httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
| 这个时候2~5都是on,就表明会自动启动了。
|------------------------------------
*/
2.修改 /etc/rc.d/rc.local 这个文件:
# 例如将 apache、MySQL、samba、svn 等这些服务的开机自启动问题一起搞定:
vi /etc/rc.d/rc.local
#添加以下命令
/usr/sbin/apachectl start
/etc/rc.d/init.d/mysqld start
/etc/rc.d/init.d/smb start
/usr/local/subversion/bin/svnserve -d
centos7.3
1.添加开机自启服务
systemctl list-unit-files //查看所有服务情况
systemctl enable jenkins.service //设置jenkins服务为自启动服务
sysstemctl start jenkins.service //启动jenkins服务
2.添加自启脚本
在centos7中增加脚本有两种常用的方法,
以脚本autostart.sh为例:
- 脚本内容
#!/bin/bash
#description:开机自启脚本
/usr/local/tomcat/bin/startup.sh #启动tomcat
- 方法一
# 赋予脚本可执行权限(/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.d
chkconfig --add autostart.sh
chkconfig autostart.sh on
Linux内核启动顺序为
- /sbin/init进程启动,
- 然后依次执行init初始脚本,
- 运行级别脚本/etc/rc.d/rc.d,号值等于运行模式,可以在/etc/inittab中查看,最后执行/etc/rc.d/rc.local。
- 如果需要配置开机任务,可以在/etc/rc.d/rc*.d中的S**rclocal文件配置,也可以在/etc/rc.d/rc.local中配置。