Linux 系统本身包含了很多服务, CentOS6 之前系统的服务用 SysV 控制, CentOS7 改为 systemd 控制
一, chkconfig 服务管理机制
简而言之, chkconfig 就是 CentOS6 以前用来控制系统服务的工具,
常用方法举例
chkconfig --list #列出所有的系统服务.
chkconfig --add httpd #增加 httpd 服务.
chkconfig --del httpd #删除 httpd 服务.
chkconfig --level httpd 2345 on #设置 httpd 在运行级别为 2,3,4,5 的情况下都是 on(开启) 的状态.
chkconfig --list mysqld #列出 mysqld 服务设置情况.
chkconfig --level 35 mysqld on #设定 mysqld 在等级 3 和 5 为开机运行服务,--level 35 表示操作只在等级 3 和 5 执行, on 表示启动, off 表
系统开机时启动的部分服务存储在 / etc/init.d / 目录下. 我们可以把需要开机启动的服务放在这个目录下然后用 chkconfig 来管理.
1, 列出 chkconfig 管理的服务
chkconfig
chkconfig --list
2, 设定不同级别上各个服务的启动与关闭
这里先提示下级别的概念, 就硬记概念, 反正这个 CentOS7 以后版本就不用了, 我们总结一些关键词方便理解:
0 是关机,
1 是单用户, 就是我们之前修改 root 账户密码的模式,
2 是多用户模式, 但比 3 模式少了一个 nfs 服务
3 是多用户命令行模式, 最常用
4 是保留级别暂时没用,
5 是图形模式,
6 是重启,
关闭 network 服务
chkconfig network off
k 开启 network 服务
chkconfig network on
关闭第 5 级别的 network 服务
chkconfig --level 5 network off
自定义一个服务并把它加入 chkconfig 管理
首先自定义的脚本要跟原有的 network 和 netconsole 服务一样格式的服务脚本.
我们看下脚本的写法
vi /etc/init.d/network
红框的部分是必须有的
我们可以把 network 复制一份然后做实验
cp network test
然后我们把命令加入 chkconfig 管理
chkconfig --add test #注意两个减号
chkconfig --del test
随着版本更新, chkconfig 已经是过去时了...
二, systemd 服务管理机制∴
查看 systemd 管理的服务
systemctl list-unit -all-type=service #查看所有服务
systemctl list-units --type=service #查看所有已经启动的服务
服务的选项有三种是否 LOAD,ACTIVE,SUB, 具体含义见下图:
针对单一服务的操作命令:
systemctl enable crond ## 设置开机启动 crond 服务或工具
systemctl disable crond ## 设置关闭开机启动 crond 服务或工具
systemctl status crond ## 查看 crond 服务当前状态, 如是否运行
systemctl stop crond ## 停止 crond 服务是, 但开机仍会运行
systemctl start crond ## 开启 crond 服务
systemctl restart crond ## 重启 crond 服务
systemctl is-enabled crond ## 检查 crond 服务是否开机启动
来源: http://www.bubuko.com/infodetail-2480143.html