Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标准配置。
本文介绍它的基本用法,分为上下两篇。今天介绍它的主要命令,下一篇介绍如何用于实战。
一、由来
下面的命令用来启动服务。
12345 $ sudo <span class="token operator">/</span>etc<span class="token operator">/</span>init<span class="token punctuation">.</span>d<span class="token operator">/</span>apache2 start<span class="token comment" spellcheck="true"># 或者</span>$ service apache2 start
这种方法有两个缺点。
一是启动时间长。init
进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
二是启动脚本复杂。init
进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。
二、Systemd 概述
Systemd 就是为了解决这些问题而诞生的。它的设计目标是,为系统的启动和管理提供一套完整的解决方案。
根据 Linux 惯例,字母d
是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。
(上图为 Systemd 作者 Lennart Poettering)
使用了 Systemd,就不需要再用init
了。Systemd 取代了initd
,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。
123 $ systemctl <span class="token operator">--</span>version
上面的命令查看 Systemd 的版本。
Systemd 的优点是功能强大,使用方便,缺点是体系庞大,非常复杂。事实上,现在还有很多人反对使用 Systemd,理由就是它过于复杂,与操作系统的其他部分强耦合,违反”keep simple, keep stupid”的Unix 哲学。
(上图为 Systemd 架构图)
三、系统管理
Systemd 并不是一个命令,而是一组命令,涉及到系统管理的方方面面。
3.1 systemctl
systemctl
是 Systemd 的主命令,用于管理系统。
12345678910111213141516171819202122 <span class="token comment" spellcheck="true"># 重启系统</span>$ sudo systemctl reboot<span class="token comment" spellcheck="true"># 关闭系统,切断电源</span>$ sudo systemctl poweroff<span class="token comment" spellcheck="true"># CPU停止工作</span>$ sudo systemctl halt<span class="token comment" spellcheck="true"># 暂停系统</span>$ sudo systemctl suspend<span class="token comment" spellcheck="true"># 让系统进入冬眠状态</span>$ sudo systemctl hibernate<span class="token comment" spellcheck="true"># 让系统进入交互式休眠状态</span>$ sudo systemctl hybrid<span class="token operator">-</span>sleep<span class="token comment" spellcheck="true"># 启动进入救援状态(单用户状态)</span>$ sudo systemctl rescue
3.2 systemd-analyze
systemd-analyze
命令用于查看启动耗时。
12345678910111213 <span class="token comment" spellcheck="true"># 查看启动耗时</span>$ systemd<span class="token operator">-</span>analyze<span class="token comment" spellcheck="true"># 查看每个服务的启动耗时</span>$ systemd<span class="token operator">-</span>analyze blame<span class="token comment" spellcheck="true"># 显示瀑布状的启动过程流</span>$ systemd<span class="token operator">-</span>analyze critical<span class="token operator">-</span>chain<span class="token comment" spellcheck="true"># 显示指定服务的启动流</span>$ systemd<span class="token operator">-</span>analyze critical<span class="token operator">-</span>chain atd<span class="token punctuation">.</span>service
3.3 hostnamectl
hostnamectl
命令用于查看当前主机的信息。
1234567 <span class="token comment" spellcheck="true"># 显示当前主机的信息</span>$ hostnamectl<span class="token comment" spellcheck="true"># 设置主机名。</span>$ sudo hostnamectl <span class="token keyword">set</span><span class="token operator">-</span>hostname rhel7
3.4 localectl
localectl
命令用于查看本地化设置。
12345678 <span class="token comment" spellcheck="true"># 查看本地化设置</span>$ localectl<span class="token comment" spellcheck="true"># 设置本地化参数。</span>$ sudo localectl <span class="token keyword">set</span><span class="token operator">-</span>locale LANG<span class="token operator">=</span>en_GB<span class="token punctuation">.</span>utf8$ sudo localectl <span class="token keyword">set</span><span class="token operator">-</span>keymap en_GB
3.5 timedatectl
timedatectl
命令用于查看当前时区设置。
123456789101112 <span class="token comment" spellcheck="true"># 查看当前时区设置</span>$ timedatectl<span class="token comment" spellcheck="true"># 显示所有可用的时区</span>$ timedatectl list<span class="token operator">-</span>timezones<span class="token comment" spellcheck="true"># 设置当前时区</span>$ sudo timedatectl <span class="token keyword">set</span><span class="token operator">-</span>timezone America<span class="token operator">/</span>New_York$ sudo timedatectl <span class="token keyword">set</span><span class="token operator">-</span>time YYYY<span class="token operator">-</span>MM<span class="token operator">-</span>DD$ sudo timedatectl <span class="token keyword">set</span><span class="token operator">-</span>time HH<span class="token punctuation">:</span>MM<span class="token punctuation">:</span>SS
3.6 loginctl
loginctl
命令用于查看当前登录的用户。
12345678910 <span class="token comment" spellcheck="true"># 列出当前session</span>$ loginctl list<span class="token operator">-</span>sessions<span class="token comment" spellcheck="true"># 列出当前登录用户</span>$ loginctl list<span class="token operator">-</span>users<span class="token comment" spellcheck="true"># 列出显示指定用户的信息</span>$ loginctl show<span class="token operator">-</span>user ruanyf
四、Unit
4.1 含义
Systemd 可以管理所有系统资源。不同的资源统称为 Unit(单位)。
Unit 一共分成12种。
- Service unit:系统服务
- Target unit:多个 Unit 构成的一个组
- Device Unit:硬件设备
- Mount Unit:文件系统的挂载点
- Automount Unit:自动挂载点
- Path Unit:文件或路径
- Scope Unit:不是由 Systemd 启动的外部进程
- Slice Unit:进程组
- Snapshot Unit:Systemd 快照,可以切回某个快照
- Socket Unit:进程间通信的 socket
- Swap Unit:swap 文件
- Timer Unit:定时器
systemctl list-units
命令可以查看当前系统的所有 Unit 。
12345678910111213141516 <span class="token comment" spellcheck="true"># 列出正在运行的 Unit</span>$ systemctl list<span class="token operator">-</span>units<span class="token comment" spellcheck="true"># 列出所有Unit,包括没有找到配置文件的或者启动失败的</span>$ systemctl list<span class="token operator">-</span>units <span class="token operator">--</span>all<span class="token comment" spellcheck="true"># 列出所有没有运行的 Unit</span>$ systemctl list<span class="token operator">-</span>units <span class="token operator">--</span>all <span class="token operator">--</span>state<span class="token operator">=</span>inactive<span class="token comment" spellcheck="true"># 列出所有加载失败的 Unit</span>$ systemctl list<span class="token operator">-</span>units <span class="token operator">--</span>failed<span class="token comment" spellcheck="true"># 列出所有正在运行的、类型为 service 的 Unit</span>$ systemctl list<span class="token operator">-</span>units <span class="token operator">--</span>type<span class="token operator">=</span>service
4.2 Unit 的状态
systemctl status
命令用于查看系统状态和单个 Unit 的状态。
12345678910 <span class="token comment" spellcheck="true"># 显示系统状态</span>$ systemctl status<span class="token comment" spellcheck="true"># 显示单个 Unit 的状态</span>$ sysystemctl status bluetooth<span class="token punctuation">.</span>service<span class="token comment" spellcheck="true"># 显示远程主机的某个 Unit 的状态</span>$ systemctl <span class="token operator">-</span>H <a class="token email-link" href="mailto:root@rhel">root@rhel</a>7<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com status httpd<span class="token punctuation">.</span>service
除了status
命令,systemctl
还提供了三个查询状态的简单方法,主要供脚本内部的判断语句使用。
12345678910 <span class="token comment" spellcheck="true"># 显示某个 Unit 是否正在运行</span>$ systemctl is<span class="token operator">-</span>active application<span class="token punctuation">.</span>service<span class="token comment" spellcheck="true"># 显示某个 Unit 是否处于启动失败状态</span>$ systemctl is<span class="token operator">-</span>failed application<span class="token punctuation">.</span>service<span class="token comment" spellcheck="true"># 显示某个 Unit 服务是否建立了启动链接</span>$ systemctl is<span class="token operator">-</span>enabled application<span class="token punctuation">.</span>service
4.3 Unit 管理
对于用户来说,最常用的是下面这些命令,用于启动和停止 Unit(主要是 service)。
12345678910111213141516171819202122232425262728 <span class="token comment" spellcheck="true"># 立即启动一个服务</span>$ sudo systemctl start apache<span class="token punctuation">.</span>service<span class="token comment" spellcheck="true"># 立即停止一个服务</span>$ sudo systemctl stop apache<span class="token punctuation">.</span>service<span class="token comment" spellcheck="true"># 重启一个服务</span>$ sudo systemctl restart apache<span class="token punctuation">.</span>service<span class="token comment" spellcheck="true"># 杀死一个服务的所有子进程</span>$ sudo systemctl kill apache<span class="token punctuation">.</span>service<span class="token comment" spellcheck="true"># 重新加载一个服务的配置文件</span>$ sudo systemctl reload apache<span class="token punctuation">.</span>service<span class="token comment" spellcheck="true"># 重载所有修改过的配置文件</span>$ sudo systemctl daemon<span class="token operator">-</span>reload<span class="token comment" spellcheck="true"># 显示某个 Unit 的所有底层参数</span>$ systemctl show httpd<span class="token punctuation">.</span>service<span class="token comment" spellcheck="true"># 显示某个 Unit 的指定属性的值</span>$ systemctl show <span class="token operator">-</span>p CPUShares httpd<span class="token punctuation">.</span>service<span class="token comment" spellcheck="true"># 设置某个 Unit 的指定属性</span>$ sudo systemctl <span class="token keyword">set</span><span class="token operator">-</span>property httpd<span class="token punctuation">.</span>service CPUShares<span class="token operator">=</span><span class="token number">500</span>
4.4 依赖关系
Unit 之间存在依赖关系:A 依赖于 B,就意味着 Systemd 在启动 A 的时候,同时会去启动 B。
systemctl list-dependencies
命令列出一个 Unit 的所有依赖。
123 $ systemctl list<span class="token operator">-</span>dependencies nginx<span class="token punctuation">.</span>service
上面命令的输出结果之中,有些依赖是 Target 类型(详见下文),默认不会展开显示。如果要展开 Target,就需要使用--all
参数。
123 $ systemctl list<span class="token operator">-</span>dependencies <span class="token operator">--</span>all nginx<span class="token punctuation">.</span>service
五、Unit 的配置文件
5.1 概述
每一个 Unit 都有一个配置文件,告诉 Systemd 怎么启动这个 Unit 。
Systemd 默认从目录/etc/systemd/system/
读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录/usr/lib/systemd/system/
,真正的配置文件存放在那个目录。
systemctl enable
命令用于在上面两个目录之间,建立符号链接关系。
12345 $ sudo systemctl enable <a class="token email-link" href="mailto:clamd@scan">clamd@scan</a><span class="token punctuation">.</span>service<span class="token comment" spellcheck="true"># 等同于</span>$ sudo ln <span class="token operator">-</span>s <span class="token string">'/<a class="token email-link" href="mailto:usr/lib/systemd/system/clamd@scan.service">usr/lib/systemd/system/clamd@scan.service</a>'</span> <span class="token string">'/<a class="token email-link" href="mailto:etc/systemd/system/multi-user.target.wants/clamd@scan.service">etc/systemd/system/multi-user.target.wants/clamd@scan.service</a>'</span>
如果配置文件里面设置了开机启动,systemctl enable
命令相当于激活开机启动。
与之对应的,systemctl disable
命令用于在两个目录之间,撤销符号链接关系,相当于撤销开机启动。
123 $ sudo systemctl disable <a class="token email-link" href="mailto:clamd@scan">clamd@scan</a><span class="token punctuation">.</span>service
配置文件的后缀名,就是该 Unit 的种类,比如sshd.socket
。如果省略,Systemd 默认后缀名为.service
,所以sshd
会被理解成sshd.service
。
5.2 配置文件的状态
systemctl list-unit-files
命令用于列出所有配置文件。
1234567 <span class="token comment" spellcheck="true"># 列出所有配置文件</span>$ systemctl list<span class="token operator">-</span>unit<span class="token operator">-</span>files<span class="token comment" spellcheck="true"># 列出指定类型的配置文件</span>$ systemctl list<span class="token operator">-</span>unit<span class="token operator">-</span>files <span class="token operator">--</span>type<span class="token operator">=</span>service
这个命令会输出一个列表。
12345678 $ systemctl list<span class="token operator">-</span>unit<span class="token operator">-</span>filesUNIT FILE STATEchronyd<span class="token punctuation">.</span>service enabledclamd@<span class="token punctuation">.</span>service static<a class="token email-link" href="mailto:clamd@scan">clamd@scan</a><span class="token punctuation">.</span>service disabled
这个列表显示每个配置文件的状态,一共有四种。
- enabled:已建立启动链接
- disabled:没建立启动链接
- static:该配置文件没有
[Install]
部分(无法执行),只能作为其他配置文件的依赖- masked:该配置文件被禁止建立启动链接
注意,从配置文件的状态无法看出,该 Unit 是否正在运行。这必须执行前面提到的systemctl status
命令。
123 $ systemctl status bluetooth<span class="token punctuation">.</span>service
一旦修改配置文件,就要让 SystemD 重新加载配置文件,然后重新启动,否则修改不会生效。
1234 $ sudo systemctl daemon<span class="token operator">-</span>reload$ sudo systemctl restart httpd<span class="token punctuation">.</span>service
5.3 配置文件的格式
配置文件就是普通的文本文件,可以用文本编辑器打开。
systemctl cat
命令可以查看配置文件的内容。
12345678910111213 $ systemctl cat atd<span class="token punctuation">.</span>service<span class="token punctuation">[</span>Unit<span class="token punctuation">]</span>Description<span class="token operator">=</span>ATD daemon<span class="token punctuation">[</span>Service<span class="token punctuation">]</span>Type<span class="token operator">=</span>forkingExecStart<span class="token operator">=</span><span class="token operator">/</span>usr<span class="token operator">/</span>bin<span class="token operator">/</span>atd<span class="token punctuation">[</span>Install<span class="token punctuation">]</span>WantedBy<span class="token operator">=</span>multi<span class="token operator">-</span>user<span class="token punctuation">.</span>target
从上面的输出可以看到,配置文件分成几个区块。每个区块的第一行,是用方括号表示的区别名,比如[Unit]
。注意,配置文件的区块名和字段名,都是大小写敏感的。
每个区块内部是一些等号连接的键值对。
1234567 <span class="token punctuation">[</span>Section<span class="token punctuation">]</span>Directive1<span class="token operator">=</span>valueDirective2<span class="token operator">=</span>value<span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span>
注意,键值对的等号两侧不能有空格。
5.4 配置文件的区块
[Unit]
区块通常是配置文件的第一个区块,用来定义 Unit 的元数据,以及配置与其他 Unit 的关系。它的主要字段如下。
Description
:简短描述Documentation
:文档地址Requires
:当前 Unit 依赖的其他 Unit,如果它们没有运行,当前 Unit 会启动失败Wants
:与当前 Unit 配合的其他 Unit,如果它们没有运行,当前 Unit 不会启动失败BindsTo
:与Requires
类似,它指定的 Unit 如果退出,会导致当前 Unit 停止运行Before
:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之后启动After
:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之前启动Conflicts
:这里指定的 Unit 不能与当前 Unit 同时运行Condition...
:当前 Unit 运行必须满足的条件,否则不会运行Assert...
:当前 Unit 运行必须满足的条件,否则会报启动失败
[Install]
通常是配置文件的最后一个区块,用来定义如何启动,以及是否开机启动。它的主要字段如下。
WantedBy
:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system
目录下面以 Target 名 +.wants
后缀构成的子目录中RequiredBy
:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system
目录下面以 Target 名 +.required
后缀构成的子目录中Alias
:当前 Unit 可用于启动的别名Also
:当前 Unit 激活(enable)时,会被同时激活的其他 Unit
[Service]
区块用来 Service 的配置,只有 Service 类型的 Unit 才有这个区块。它的主要字段如下。
Type
:定义启动时的进程行为。它有以下几种值。Type=simple
:默认值,执行ExecStart
指定的命令,启动主进程Type=forking
:以 fork 方式从父进程创建子进程,创建后父进程会立即退出Type=oneshot
:一次性进程,Systemd 会等当前服务退出,再继续往下执行Type=dbus
:当前服务通过D-Bus启动Type=notify
:当前服务启动完毕,会通知Systemd
,再继续往下执行Type=idle
:若有其他任务执行完毕,当前服务才会运行ExecStart
:启动当前服务的命令ExecStartPre
:启动当前服务之前执行的命令ExecStartPost
:启动当前服务之后执行的命令ExecReload
:重启当前服务时执行的命令ExecStop
:停止当前服务时执行的命令ExecStopPost
:停止当其服务之后执行的命令RestartSec
:自动重启当前服务间隔的秒数Restart
:定义何种情况 Systemd 会自动重启当前服务,可能的值包括always
(总是重启)、on-success
、on-failure
、on-abnormal
、on-abort
、on-watchdog
TimeoutSec
:定义 Systemd 停止当前服务之前等待的秒数Environment
:指定环境变量
Unit 配置文件的完整字段清单,请参考官方文档。
六、Target
启动计算机的时候,需要启动大量的 Unit。如果每一次启动,都要一一写明本次启动需要哪些 Unit,显然非常不方便。Systemd 的解决方案就是 Target。
简单说,Target 就是一个 Unit 组,包含许多相关的 Unit 。启动某个 Target 的时候,Systemd 就会启动里面所有的 Unit。从这个意义上说,Target 这个概念类似于”状态点”,启动某个 Target 就好比启动到某种状态。
传统的init
启动模式里面,有 RunLevel 的概念,跟 Target 的作用很类似。不同的是,RunLevel 是互斥的,不可能多个 RunLevel 同时启动,但是多个 Target 可以同时启动。
123456789101112131415161718 <span class="token comment" spellcheck="true"># 查看当前系统的所有 Target</span>$ systemctl list<span class="token operator">-</span>unit<span class="token operator">-</span>files <span class="token operator">--</span>type<span class="token operator">=</span>target<span class="token comment" spellcheck="true"># 查看一个 Target 包含的所有 Unit</span>$ systemctl list<span class="token operator">-</span>dependencies multi<span class="token operator">-</span>user<span class="token punctuation">.</span>target<span class="token comment" spellcheck="true"># 查看启动时的默认 Target</span>$ systemctl get<span class="token operator">-</span>default<span class="token comment" spellcheck="true"># 设置启动时的默认 Target</span>$ sudo systemctl <span class="token keyword">set</span><span class="token operator">-</span>default multi<span class="token operator">-</span>user<span class="token punctuation">.</span>target<span class="token comment" spellcheck="true"># 切换 Target 时,默认不关闭前一个 Target 启动的进程,</span><span class="token comment" spellcheck="true"># systemctl isolate 命令改变这种行为,</span><span class="token comment" spellcheck="true"># 关闭前一个 Target 里面所有不属于后一个 Target 的进程</span>$ sudo systemctl isolate multi<span class="token operator">-</span>user<span class="token punctuation">.</span>target
Target 与 传统 RunLevel 的对应关系如下。
1234567891011 Traditional runlevel New <span class="token class-name">target</span> name Symbolically linked to<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>Runlevel <span class="token number">0</span> <span class="token operator">|</span> runlevel0<span class="token punctuation">.</span>target <span class="token operator">-</span><span class="token operator">></span> poweroff<span class="token punctuation">.</span>targetRunlevel <span class="token number">1</span> <span class="token operator">|</span> runlevel1<span class="token punctuation">.</span>target <span class="token operator">-</span><span class="token operator">></span> rescue<span class="token punctuation">.</span>targetRunlevel <span class="token number">2</span> <span class="token operator">|</span> runlevel2<span class="token punctuation">.</span>target <span class="token operator">-</span><span class="token operator">></span> multi<span class="token operator">-</span>user<span class="token punctuation">.</span>targetRunlevel <span class="token number">3</span> <span class="token operator">|</span> runlevel3<span class="token punctuation">.</span>target <span class="token operator">-</span><span class="token operator">></span> multi<span class="token operator">-</span>user<span class="token punctuation">.</span>targetRunlevel <span class="token number">4</span> <span class="token operator">|</span> runlevel4<span class="token punctuation">.</span>target <span class="token operator">-</span><span class="token operator">></span> multi<span class="token operator">-</span>user<span class="token punctuation">.</span>targetRunlevel <span class="token number">5</span> <span class="token operator">|</span> runlevel5<span class="token punctuation">.</span>target <span class="token operator">-</span><span class="token operator">></span> graphical<span class="token punctuation">.</span>targetRunlevel <span class="token number">6</span> <span class="token operator">|</span> runlevel6<span class="token punctuation">.</span>target <span class="token operator">-</span><span class="token operator">></span> reboot<span class="token punctuation">.</span>target
它与init
进程的主要差别如下。
(1)默认的 RunLevel(在
/etc/inittab
文件设置)现在被默认的 Target 取代,位置是/etc/systemd/system/default.target
,通常符号链接到graphical.target
(图形界面)或者multi-user.target
(多用户命令行)。(2)启动脚本的位置,以前是
/etc/init.d
目录,符号链接到不同的 RunLevel 目录 (比如/etc/rc3.d
、/etc/rc5.d
等),现在则存放在/lib/systemd/system
和/etc/systemd/system
目录。(3)配置文件的位置,以前
init
进程的配置文件是/etc/inittab
,各种服务的配置文件存放在/etc/sysconfig
目录。现在的配置文件主要存放在/lib/systemd
目录,在/etc/systemd
目录里面的修改可以覆盖原始设置。
七、日志管理
Systemd 统一管理所有 Unit 的启动日志。带来的好处就是,可以只用journalctl
一个命令,查看所有日志(内核日志和应用日志)。日志的配置文件是/etc/systemd/journald.conf
。
journalctl
功能强大,用法非常多。
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 <span class="token comment" spellcheck="true"># 查看所有日志(默认情况下 ,只保存本次启动的日志)</span>$ sudo journalctl<span class="token comment" spellcheck="true"># 查看内核日志(不显示应用日志)</span>$ sudo journalctl <span class="token operator">-</span>k<span class="token comment" spellcheck="true"># 查看系统本次启动的日志</span>$ sudo journalctl <span class="token operator">-</span>b$ sudo journalctl <span class="token operator">-</span>b <span class="token operator">-</span><span class="token number">0</span><span class="token comment" spellcheck="true"># 查看上一次启动的日志(需更改设置)</span>$ sudo journalctl <span class="token operator">-</span>b <span class="token operator">-</span><span class="token number">1</span><span class="token comment" spellcheck="true"># 查看指定时间的日志</span>$ sudo journalctl <span class="token operator">--</span>since<span class="token operator">=</span><span class="token string">"2012-10-30 18:17:16"</span>$ sudo journalctl <span class="token operator">--</span>since <span class="token string">"20 min ago"</span>$ sudo journalctl <span class="token operator">--</span>since yesterday$ sudo journalctl <span class="token operator">--</span>since <span class="token string">"2015-01-10"</span> <span class="token operator">--</span><span class="token keyword">until</span> <span class="token string">"2015-01-11 03:00"</span>$ sudo journalctl <span class="token operator">--</span>since <span class="token number">09</span><span class="token punctuation">:</span><span class="token number">00</span> <span class="token operator">--</span><span class="token keyword">until</span> <span class="token string">"1 hour ago"</span><span class="token comment" spellcheck="true"># 显示尾部的最新10行日志</span>$ sudo journalctl <span class="token operator">-</span>n<span class="token comment" spellcheck="true"># 显示尾部指定行数的日志</span>$ sudo journalctl <span class="token operator">-</span>n <span class="token number">20</span><span class="token comment" spellcheck="true"># 实时滚动显示最新日志</span>$ sudo journalctl <span class="token operator">-</span>f<span class="token comment" spellcheck="true"># 查看指定服务的日志</span>$ sudo journalctl <span class="token operator">/</span>usr<span class="token operator">/</span>lib<span class="token operator">/</span>systemd<span class="token operator">/</span>systemd<span class="token comment" spellcheck="true"># 查看指定进程的日志</span>$ sudo journalctl _PID<span class="token operator">=</span><span class="token number">1</span><span class="token comment" spellcheck="true"># 查看某个路径的脚本的日志</span>$ sudo journalctl <span class="token operator">/</span>usr<span class="token operator">/</span>bin<span class="token operator">/</span>bash<span class="token comment" spellcheck="true"># 查看指定用户的日志</span>$ sudo journalctl _UID<span class="token operator">=</span><span class="token number">33</span> <span class="token operator">--</span>since today<span class="token comment" spellcheck="true"># 查看某个 Unit 的日志</span>$ sudo journalctl <span class="token operator">-</span>u nginx<span class="token punctuation">.</span>service$ sudo journalctl <span class="token operator">-</span>u nginx<span class="token punctuation">.</span>service <span class="token operator">--</span>since today<span class="token comment" spellcheck="true"># 实时滚动显示某个 Unit 的最新日志</span>$ sudo journalctl <span class="token operator">-</span>u nginx<span class="token punctuation">.</span>service <span class="token operator">-</span>f<span class="token comment" spellcheck="true"># 合并显示多个 Unit 的日志</span>$ journalctl <span class="token operator">-</span>u nginx<span class="token punctuation">.</span>service <span class="token operator">-</span>u php<span class="token operator">-</span>fpm<span class="token punctuation">.</span>service <span class="token operator">--</span>since today<span class="token comment" spellcheck="true"># 查看指定优先级(及其以上级别)的日志,共有8级</span><span class="token comment" spellcheck="true"># 0: emerg</span><span class="token comment" spellcheck="true"># 1: alert</span><span class="token comment" spellcheck="true"># 2: crit</span><span class="token comment" spellcheck="true"># 3: err</span><span class="token comment" spellcheck="true"># 4: warning</span><span class="token comment" spellcheck="true"># 5: notice</span><span class="token comment" spellcheck="true"># 6: info</span><span class="token comment" spellcheck="true"># 7: debug</span>$ sudo journalctl <span class="token operator">-</span>p err <span class="token operator">-</span>b<span class="token comment" spellcheck="true"># 日志默认分页输出,--no-pager 改为正常的标准输出</span>$ sudo journalctl <span class="token operator">--</span>no<span class="token operator">-</span>pager<span class="token comment" spellcheck="true"># 以 JSON 格式(单行)输出</span>$ sudo journalctl <span class="token operator">-</span>b <span class="token operator">-</span>u nginx<span class="token punctuation">.</span>service <span class="token operator">-</span>o json<span class="token comment" spellcheck="true"># 以 JSON 格式(多行)输出,可读性更好</span>$ sudo journalctl <span class="token operator">-</span>b <span class="token operator">-</span>u nginx<span class="token punctuation">.</span>serviceqq<span class="token operator">-</span>o json<span class="token operator">-</span>pretty<span class="token comment" spellcheck="true"># 显示日志占据的硬盘空间</span>$ sudo journalctl <span class="token operator">--</span>disk<span class="token operator">-</span>usage<span class="token comment" spellcheck="true"># 指定日志文件占据的最大空间</span>$ sudo journalctl <span class="token operator">--</span>vacuum<span class="token operator">-</span>size<span class="token operator">=</span>1G<span class="token comment" spellcheck="true"># 指定日志文件保存多久</span>$ sudo journalctl <span class="token operator">--</span>vacuum<span class="token operator">-</span>time<span class="token operator">=</span>1years
转自:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html
0 Comments