Systemd 简介

以下我把可以管理系统所有进程、服务以及启动项等的软件简称「系统管理器」。

在 CentOS 7 之前,系统以 System 来作为系统管理器。

System V 有一个致命的缺点就是过度依赖于脚本来实现服务管理,从而导致服务几乎没办法并行启动,最终导致系统启动效率较为低下。

从 CentOS 7 开始,Systemd 成为新的系统管理器。我认为它最大的优点就是支持进服务并行启动,从而使效率大大提高;同时它还具有日志管理、快照备份与恢复、挂载点管理等多种实用功能,功能甩 System V 几条街!

而且 systemd 进程的 PID 是 1 ,也就是说 Systemd 掌管着一切进程!

当然了 Systemd 是向下兼容 System V 的。

以下只介绍 Systemd 的服务、启动项和日志管理这三项功能,其他功能不涉及。

说明

  1. 下文提到的服务项名称后面的 .service 可以省略不写,系统会自动补全。
  2. Systemd 不仅仅管理系统的服务项,还能管理着挂载点、套接字等。每一个 Systemd 管理项称为 unit,unit可以有很多类型。
  3. 本文仅介绍 .service 类型和 .target 的 unit本文适用于所有使用 Systemd 的操作系统,不局限于 CentOS 7。

 服务、系统状态的查看

1. 查看系统所有安装的服务项

systemctl list-unit-files --type=service

使用 PageUp 或 PageDown 翻页,查看完毕后按 q 退出。

2. 查看系统所有运行的服务

systemctl list-units --type=service 

如果看到某个服务项前面有一个红点,说明该服务存在问题,请进行排查。

使用 PageUp 或 PageDown 翻页,查看完毕后按 q 退出。

3. 查看系统所有开机自启动的服务项

systemctl list-unit-files --type=service |  grep enabled

4. 查看指定服务项状态

systemctl status <服务项名称>

执行命令之后,系统会显示该服务项的状态、是否已激活、描述以及最后十条日志。

如果服务项前面有一个红点,说明该服务存在问题,请根据日志进行排查。

例如

查看 Nginx 服务状态

[root: ~]# systemctl status nginx.service

● nginx.service - nginx - high performance web server

 Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)

 Active: inactive (dead)

 Docs: http://nginx.org/en/docs/

9月 05 09:24:07 CentOS_VM systemd[1]: nginx.service: control process exited, code=exited status=1

9月 05 09:24:07 CentOS_VM systemd[1]: Failed to start nginx - high performance web server.

9月 05 09:24:07 CentOS_VM systemd[1]: Unit nginx.service entered failed state.

9月 05 09:24:07 CentOS_VM systemd[1]: nginx.service failed.

9月 05 09:28:39 CentOS_VM systemd[1]: Starting nginx - high performance web server...

9月 05 09:28:39 CentOS_VM nginx[5566]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

9月 05 09:28:39 CentOS_VM nginx[5566]: nginx: configuration file /etc/nginx/nginx.conf test is successful

9月 05 09:28:39 CentOS_VM systemd[1]: Started nginx - high performance web server.

9月 05 09:28:49 CentOS_VM systemd[1]: Stopping nginx - high performance web server...

9月 05 09:28:49 CentOS_VM systemd[1]: Stopped nginx - high performance web server.
View Code

相关文章: