s.k*_*s.k 18 command-line log systemd-journald
我想按日期过滤系统日志文件,即当我这样做时:
$ cat /var/log/syslog | grep -i "error\|warn\|kernel"
Run Code Online (Sandbox Code Playgroud)
它在最后三天打印这样的行让我们说:
(...)
Apr 3 06:17:38 computer_name kernel: [517239.805470] IPv6: ADDRCONF(NETDEV_CHANGE): wlp3s0: link becomes ready
(...)
Apr 4 19:34:21 computer_name kernel: [517242.523165] e1000e: enp0s25 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
(...)
Apr 5 09:00:52 computer_name kernel: [517242.523217] IPv6: ADDRCONF(NETDEV_CHANGE): enp0s25: link becomes ready
Run Code Online (Sandbox Code Playgroud)
如何 grep(选择或过滤):
$ cat /var/log/syslog | grep -i "Apr 5" | grep -i "error\|warn\|kernel"
Run Code Online (Sandbox Code Playgroud)
它在syslog文件上按预期工作,但不适用于kern.log文件,例如只返回:Binary file (standard input) matches。当我tail看到这个特定文件时,我可以看到与文件中相同的起始日期格式syslog。
如何在kern.log文件等其他日志上实现相同的目标?
此外,是否可以过滤:
提示:如果可能,使用“易于记忆的命令”。
tom*_*chi 23
使用 systemd,我们得到了 journalctl,它可以轻松实现这样的细粒度过滤:
journalctl --since "2 days ago"
journalctl --since "2019-03-10" --until "2019-03-11 03:00"
journalctl -b # last boot
journalctl -k # kernel messages
journalctl -p er # by priority (emerg|alert|crit|err|warning|info|debug)
journalctl -u sshd # by unit
journalctl _UID=1000 # by user id
Run Code Online (Sandbox Code Playgroud)
例子可以结合!
一般来说,它kern.log是一个文本文件。但有时会发生它包含一些二进制数据,特别是当系统之前崩溃并且系统无法正常关闭文件时。然后您可能会注意到包含诸如此类的文本的行^@^@^@^@^@^@^@^@^@。
如果grep注意到它的输入是binary,它通常会停止进一步处理并打印出来... binary file ...。但是有一个开关可以改变这种行为。从联机帮助页:
Run Code Online (Sandbox Code Playgroud)[...] File and Directory Selection -a, --text Process a binary file as if it were text; this is equivalent to the --binary-files=text option. [...]
您可以尝试以下操作:
$ grep -a -i "Apr 5" /var/log/kern.log | grep -i "error\|warn\|kernel"
Run Code Online (Sandbox Code Playgroud)
(但我实际上更喜欢journalctl另一个答案中给出的解决方案。)
| 归档时间: |
|
| 查看次数: |
23274 次 |
| 最近记录: |