systemctl:按单元名称中的模式过滤

systemctl:按单元名称中的模式过滤

当不带参数执行时,systemctl输出一个漂亮的表格,显示系统中每个单元的状态:

$ systemctl

  UNIT                       LOAD   ACTIVE     SUB       DESCRIPTION
  console-setup.service      loaded active     exited    Set console font and keymap
  cron.service               loaded active     running   Regular background program processing daemon
  dbus.service               loaded active     running   D-Bus System Message Bus
  myapp-component1.service   loaded active     running   Application - Component 1
● myapp-component2.service   loaded active     failed    Application - Component 2
  [...]
  dbus.socket                loaded active     running   D-Bus System Message Bus Socket
  myapp-socket1.socket       loaded active     listening Application - a socket
  [...]
  fstrim.timer               loaded active     waiting   Discard unused blocks once a week
  myapp-periodic.timer       loaded active     waiting   Application - periodic job
  [...]

我的应用程序由一系列由 systemd 协调的服务(以及计时器、套接字等)组成。对于应用程序的每个子组件,单元名称以“myapp-”开头。我想用systemctl它来一目了然地观察我的应用程序组件的状态。

有没有办法要求systemctl仅列出名称与模式匹配的单位(例如'myapp-*')?

$ systemctl <some-option-asking-for 'myapp-*`>

  UNIT                       LOAD   ACTIVE     SUB       DESCRIPTION
  myapp-component1.service   loaded active     running   Application - Component 1
● myapp-component2.service   loaded active     failed    Application - Component 2
  myapp-socket1.socket       loaded active     listening Application - a socket
  myapp-periodic.timer       loaded active     waiting   Application - periodic job

笔记systemctl:我对一种要求自己过滤结果集的方法感兴趣。 Grepping 和重新格式化(例如通过column -t)完整的systemctl输出有我宁愿避免的边缘情况。

答案1

systemctl'd 默认命令是list-units,并且需要一个模式作为可选参数,所以

systemctl list-units 'myapp-*'

会做你想做的事。

许多systemctl命令都支持模式;例如

systemctl status 'myapp-*'

将显示以“myapp-”开头的所有单元的状态。

相关内容