sti*_*tiv 2 grep colors systemctl
如果我在没有 grep 的情况下调用命令 systemctl,则格式看起来正确:
现在我只想过滤带有 'yarn' 的行,但这个时间线分为两部分:
当控制台宽两倍时也会发生同样的情况。我怎样才能解决这个问题?
当systemctl的输出未发送到终端时,例如,如果它通过管道传输到grep,则它允许其输出的每一列根据需要增长以适应列表中最长的项目。这就是这里发生的事情:单元列表中的某些单元具有很长的名称,并且对齐所有单元的输出会在yarn.service和之间产生很大的空间loaded failed failed。
要过滤单元输出,请使用systemctl的内置模式匹配:
sudo systemctl list-units '*yarn*'
Run Code Online (Sandbox Code Playgroud)
您可以将输出限制为grep使用该--no-legend选项获得的行,避免使用该选项着色和特殊字符--plain,并避免使用该--no-pager选项启动寻呼机:
sudo systemctl list-units --plain --no-legend --no-pager '*yarn*'
Run Code Online (Sandbox Code Playgroud)
您可以指定多个模式,结果将包括与任何模式的任何匹配:
sudo systemctl list-units --plain --no-legend --no-pager '*yarn*' '*hdfs*'
Run Code Online (Sandbox Code Playgroud)
如果要检索特定单位的状态,还有更好的子命令;参见例如测试服务是否在脚本中运行的“正确”方法。