chkconfig:管理开机服务

xiaohai 2021-07-24 16:43:17 3165人围观 标签: Linux 
简介chkconfig命令是Redhat系列的Linux系统中的系统服务管理工具,他们可以用于查询和更新不同的运行等级下系统服务的启动状态
说明

  chkconfig命令是Redhat系列的Linux系统中的系统服务管理工具,他们可以用于查询和更新不同的运行等级下系统服务的启动状态

格式

  chkconfig [option]

常用参数
  • —list:显示不同运行级别下服务的启动状态
  • —add:添加一个系统服务
  • —del:删除一个系统服务
  • —level:指定运行级别
示例

1、查看系统的服务状态

[root@localhost ~]# chkconfig --list #查看所有的服务状态

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

iprdump            0:off    1:off    2:on    3:on    4:on    5:on    6:off
iprinit            0:off    1:off    2:on    3:on    4:on    5:on    6:off
iprupdate          0:off    1:off    2:on    3:on    4:on    5:on    6:off
mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off
netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off


[root@localhost ~]# chkconfig --list mysqld #指定服务名显示服务状态

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off

在Centos7上,已经不建议使用chkconfig,而是使用systemctl

  • 0:关机
  • 1:单用户模式
  • 2:没有网络的多用户模式
  • 3:完全的多用户模式
  • 4:没有使用的级别
  • 5:图形界面多用户模式
  • 6:重启

2、管理系统服务

[root@localhost ~]# chkconfig --list mysqld #2345级别都是启动状态
mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@localhost ~]# chkconfig mysqld off #关闭2345级别的开机启动
[root@localhost ~]# chkconfig --list mysqld #再查看
mysqld             0:off    1:off    2:off    3:off    4:off    5:off    6:off
[root@localhost ~]# chkconfig mysqld on #开启2345级别的开机启动
[root@localhost ~]# chkconfig --list mysqld
mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off

3、了解chkconfig的原理
chkconfig的原理是在runlevel级别的/etc/rc.d/rc*.d目录中将对应的服务作为一个S(start启动)或K(kill停止)开头的链接

[root@localhost ~]# ll /etc/rc.d/
total 8
drwxr-xr-x. 2 root root 4096 May  5 14:49 init.d
drwxr-xr-x. 2 root root  112 Jul  1 10:44 rc0.d
drwxr-xr-x. 2 root root  112 Jul  1 10:44 rc1.d
drwxr-xr-x. 2 root root  112 Jul  1 10:44 rc2.d
drwxr-xr-x. 2 root root  112 Jul  1 10:44 rc3.d
drwxr-xr-x. 2 root root  112 Jul  1 10:44 rc4.d
drwxr-xr-x. 2 root root  112 Jul  1 10:44 rc5.d
drwxr-xr-x. 2 root root  112 Jul  1 10:44 rc6.d
-rwxr-xr-x. 1 root root  582 May  8 04:00 rc.local


[root@localhost ~]# ll /etc/rc.d/rc4.d/ #在4运行级别上mysql是S开头的
total 0
lrwxrwxrwx. 1 root root 20 May  5 13:17 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 17 May  5 13:47 S10network -> ../init.d/network
lrwxrwxrwx. 1 root root 17 May  5 13:18 S20iprinit -> ../init.d/iprinit
lrwxrwxrwx. 1 root root 19 May  5 13:18 S20iprupdate -> ../init.d/iprupdate
lrwxrwxrwx. 1 root root 17 Jul  1 10:44 S21iprdump -> ../init.d/iprdump
lrwxrwxrwx. 1 root root 16 Jul  1 10:44 S64mysqld -> ../init.d/mysqld #mysqld是启动

[root@localhost ~]# ll /etc/rc.d/rc1.d/ #在1运行级别上mysql是K开头的
total 0
lrwxrwxrwx. 1 root root 16 Jul  1 10:44 K36mysqld -> ../init.d/mysqld #mysqld是停止
lrwxrwxrwx. 1 root root 20 May  5 13:17 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 17 Jul  1 10:44 K79iprdump -> ../init.d/iprdump
lrwxrwxrwx. 1 root root 17 May  5 13:18 K80iprinit -> ../init.d/iprinit
lrwxrwxrwx. 1 root root 19 May  5 13:18 K80iprupdate -> ../init.d/iprupdate
lrwxrwxrwx. 1 root root 17 May  5 13:47 K90network -> ../init.d/network

4、写一个chkconfig命令启动的脚本
脚本的开头必须要包含下面两行

# chkconfig: 2345 20 80 #2345是运行级别,20是开机启动服务顺序,80是关机停止服务的顺序
# description: Saves and restores system entropy pool for higher quality random number generation. #启动脚本的相关描述

实验步骤:

[root@localhost ~]# cd /etc/init.d/ #进入到服务器启动脚本保存的目录/etc/init.d下
[root@localhost init.d]# vim hi917 #新建一个启动脚本,内容如下
#!/bin/sh
# chkconfig: 2345 50 88
# description: www.hi917.com chkconfig test
echo "www.hi917.com"

[root@localhost init.d]# chmod +x hi917 #脚本需要可执行权限
[root@localhost init.d]# chkconfig --add hi917 #添加到系统服务
[root@localhost init.d]# chkconfig --list hi917 #查看服务信息,添加成功
hi917              0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@localhost init.d]# chkconfig --del hi917 #删除系统服务
[root@localhost init.d]# chkconfig --list hi917 #查看,没有了
service hi917 supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add hi917')