Linux系统服务管理

5次阅读

共计 2107 个字符,预计需要花费 6 分钟才能阅读完成。

环境:CentOS Linux release 7.5.1804 (Core)
chkconfig
chkconfig 命令主要用来更新(启动或停止)和查询系统服务的运行级信息。参数用法:–add 增加所指定的系统服务,让 chkconfig 指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。–del 删除所指定的系统服务,不再由 chkconfig 指令管理,并同时在系统启动的叙述文内删除相关数据。–level< 等级代号 > 指定读系统服务要在哪一个执行等级中开启或关毕。

等级 0 表示:表示关机
等级 1 表示:单用户模式
等级 2 表示:无网络连接的多用户命令行模式
等级 3 表示:有网络连接的多用户命令行模式
等级 4 表示:不可用
等级 5 表示:带图形界面的多用户模式
等级 6 表示:重新启动

示例:chkconfig –list 显示所有运行级系统服务的运行状态信息(on 或 off)。如果指定了 name,那么只显示指定的服务在不同运行级的状态, 如(chkconfig –list nginx)。
[root@moli_linux1 ~]$ chkconfig –list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

要列出 systemd 服务,请执行 ‘systemctl list-unit-files’。
查看在具体 target 启用的服务请执行
‘systemctl list-dependencies [target]’。

mysqld 0: 关 1: 关 2: 开 3: 开 4: 开 5: 开 6: 关
netconsole 0: 关 1: 关 2: 关 3: 关 4: 关 5: 关 6: 关
network 0: 关 1: 关 2: 开 3: 开 4: 开 5: 开 6: 关
nginx 0: 关 1: 关 2: 开 3: 开 4: 开 5: 开 6: 关
php-fpm 0: 关 1: 关 2: 开 3: 开 4: 开 5: 开 6: 关

示例 2:将 network 服务 3 级别停止掉
$ chkconfig –level 3 network off
示例 3:将 network 服务 345 级别停止掉
$ chkconfig –level 345 network off
示例 4:将 network 服务从启动列表删除
$ chkconfig –del network
示例 5:将 network 服务加入到开机启动列表
$ chkconfig –add network
将一个服务或者脚本加入开机启动列表,需要将这个服务的启动文件 (shell 脚本) 放入 /etc/init.d 目录下,而且启动文件中需要有# chkconfig:2342 74 83 和# description:xxx 这样格式的存在。如:
systemd
CentOS7 中使用 systemd 替换了 SysV。Systemd 目的是要取代 Unix 时代以来一直在使用的 init 系统,兼容 SysV 和 LSB 的启动脚本,而且够在进程启动过程中更有效地引导加载服务。
如果是通过 RPM 或者 YUM 安装,则应用程序一般在 /usr/lib/systemd/system 目录下创建对应的配置文件,我们可以通过系统提供的 systemctl 命令来管理这些服务。systemd 的特性有:

支持并行化任务
同时采用 socket 式与 D -Bus 总线式激活服务;
按需启动守护进程(daemon);
利用 Linux 的 cgroups 监视进程;
支持快照和系统恢复;
维护挂载点和自动挂载点;
各服务间基于依赖关系进行精密控制。

systemctl 命令
检视和控制 systemd 的主要命令是 systemctl。该命令可用于查看系统状态和管理系统及服务。
1. 查看系统中所有服务
$ systemctl list-units –all –type=service
2. 让服务开机启动(以 crond 服务为例)
$ systemctl enable crond.service

3. 不让服务开机启动
$ systemctl disable crond

4. 查看状态
$ systemctl status crond
如图,crond 服务正在启动状态 5. 停止服务
$ systemctl stop crond

6. 启动服务
$ systemctl start crond

7. 重启服务
$ systemctl restart crond

8. 检查服务是否开机启动
$ systemctl is-enabled crond
enabled 表示开机启动,disabled 表示开机不启动
注意:设置一个服务开机启动或者开机不启动的时候,会有一个提示,例如,设置 crond 服务开机启动或者不启动,都会有一行提示如下:

# 设置开机自启
$ systemctl enable crond
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

# 设置开机不自启
$ systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
当 enable 一个服务时,就会创建一条软连接,而 disable 的时候就会挪走这个软连接。

正文完
 0