如何通过进程名称过滤 journalctl 输出?

如何通过进程名称过滤 journalctl 输出?

当一个进程记录到 systemd 时,它的进程名称会被记录并作为标识符显示在 journalctl 输出中。

$ systemd-cat echo "test"
$ journalctl -f
-- Logs begin at Sat 2018-02-24 12:13:24 CET. --
...
Jul 25 13:52:26 mycomputer echo[25098]: test

然而,根据这个名字进行过滤似乎不起作用。

$ journalctl -f -t echo
-- Logs begin at Sat 2018-02-24 12:13:24 CET. --
$ journalctl -f -u echo
-- Logs begin at Sat 2018-02-24 12:13:24 CET. --

当我手动指定一个标识符时,就会显示该标识符而不是进程名称,并且过滤有效。

$ systemd-cat -t myapp echo "test"
$ journalctl -f
-- Logs begin at Sat 2018-02-24 12:13:24 CET. --
Jul 25 13:56:08 mycomputer myapp[25213]: test
$ journalctl -f -t myapp
-- Logs begin at Sat 2018-02-24 12:13:24 CET. --
Jul 25 13:56:08 mycomputer myapp[25213]: test

如何根据进程名称过滤 journalctl 输出?

答案1

刚刚想通了。你可以使用任何systemd 日志字段通过指定作为过滤器<FIELD_NAME>=<VALUE>

以下字段对于此问题有用:

_COMM=, _EXE=, _CMDLINE=

日记帐分录源自的进程的名称、可执行路径和命令行。

因此,为了过滤命令名称,请使用journalctl -f _COMM=<command-name>

$ journalctl -f _COMM=echo
-- Logs begin at Sat 2018-02-24 12:13:24 CET. --
Jul 25 13:52:26 mycomputer echo[25098]: test
Jul 25 13:56:08 mycomputer myapp[25213]: test

相关内容