Linux journald相关

来自三线的随记
[root@compute-2 ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

[root@compute-2 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)

Links

https://www.freedesktop.org/software/systemd/man/journalctl.html

Save journal to files

- 令 journald 保存到文件中

默认journald保存到 /run/log/journal 下面,由于/run是个临时空间

所以该保存策略只有本次开机后的日志

对于某些场景的日志记录不足以满足需求(如机器异常关机,重新开机读取不了上一次的journal)


执行 mkdir /var/log/journal

等一会日志就会迁移到/var/log/journal下面了

不需要重启任何services


Save journal to /run/log

- 令 journald 只记录从开机后的信息到内存中,不保存到文件(默认)

systemctl stop systemd-journald.socket
systemctl stop systemd-journald.service

(注意区分先后顺序,先 stop systemd-journald.socket 再 stop systemd-journald.service )

因为

Warning: Stopping systemd-journald.service, but it can still be activated by:
  systemd-journald.socket
mv /var/log/journal /root/
systemctl start systemd-journald.socket

这时候 systemd-journald.service 应该也 be activated 了

journalctl --list-boots 也看到只有开机后的日志信息了(大误,是 systemctl start systemd-journald.socket 后的日志信息,因为旧的被迁走了 )


ps: 这种mv操作会导致 journal出现断层,也就是

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

有一个能够弥补一点点的方法就是(毕竟 journal stop 掉的这个中间时间段,个人觉得日志是丢了的,没有去核对)

mv /var/log/journal /run/log/
systemctl start systemd-journald.socket
journalctl --list-boots # 查看日志记录区间

其实 systemd-journald.socket 和 systemd-journald.service 其中一个执行 start 了另一个就会start了


Related environment variables

SYSTEMD_LESS

Override the options passed to less (by default "FRSXMK").

ssh 到linux节点后,export SYSTEMD_LESS=FRSMK 支持通过鼠标滚轮滑动查看 journal 日志


Related Commands

journalctl --list-boots
journalctl -b *********
journalctl -b ********* -n ***
journalctl -u ****  # -u --unit=UNIT           Show logs from the specified unit
journalctl -D  #从指定目录读取日志文件(会被整合) -D --directory=PATH      Show journal files from directory