且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中

更新时间:2022-09-29 19:18:44

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来管理。
Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中

1、列出chkconfig管理的服务

chkconfig
chkconfig --list

Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中

2、设定不同级别上各个服务的启动与关闭

这里先提示下级别的概念,就硬记概念,反正这个CentOS7以后版本就不用了,我们总结一些关键词方便理解:
0是关机,
1是单用户,就是我们之前修改root账户密码的模式,
2是多用户模式,但比3模式少了一个nfs服务
3是多用户命令行模式,最常用
4是保留级别暂时没用,
5是图形模式,
6是重启,

关闭network服务

chkconfig network off
Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中

k开启network服务

chkconfig network on
Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中

关闭第5级别的network服务

chkconfig --level 5 network off

Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中

自定义一个服务并把它加入chkconfig管理
首先自定义的脚本要跟原有的network和netconsole服务一样格式的服务脚本。
我们看下脚本的写法

vi /etc/init.d/network
Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中
红框的部分是必须有的

我们可以把network复制一份然后做实验

cp network test

Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中

然后我们把命令加入chkconfig管理

chkconfig --add test #注意两个减号

Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中

chkconfig --del test

Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中

随着版本更新,chkconfig已经是过去时了。。。

二、systemd服务管理机制∴

查看systemd管理的服务

systemctl list-unit -all-type=service #查看所有服务
systemctl list-units --type=service #查看所有已经启动的服务

Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中
服务的选项有三种是否LOAD、ACTIVE、SUB,具体含义见下图:
Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中

针对单一服务的操作命令:

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服务是否开机启动



 本文转自 whytl 51CTO博客,原文链接:

http://blog.51cto.com/11934539/2066631