从 systemctl 状态输出中删除 CGroup

从 systemctl 状态输出中删除 CGroup

我运行了 125 个启动服务器的 PHP7.0-FPM,我喜欢在重新启动后检查其状态。使用systemctl status,这当然会产生超过 130 行的输出,很难快速阅读。

root@web2:/etc/php/7.0/fpm/pool.d# systemctl --no-pager --full status php7.0-fpm
● php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2016-10-27 13:38:29 CDT; 13min ago
  Process: 5961 ExecStartPre=/usr/lib/php/php7.0-fpm-checkconf (code=exited, status=0/SUCCESS)
 Main PID: 5971 (php-fpm7.0)
   Status: "Processes active: 0, idle: 125, Requests: 12, slow: 0, Traffic: 0req/sec"
    Tasks: 126
   Memory: 294.2M
      CPU: 728ms
   CGroup: /system.slice/php7.0-fpm.service
           ├─5971 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
           ├─5974 php-fpm: pool www
           ├─5975 php-fpm: pool www
           ├─5976 php-fpm: pool www
           ├─5977 php-fpm: pool www
           ├─5978 php-fpm: pool www
           ├─5979 php-fpm: pool www
           ├─5980 php-fpm: pool www
           ├─5981 php-fpm: pool www
           ├─5982 php-fpm: pool www
           ├─5983 php-fpm: pool www
           ├─5984 php-fpm: pool www
           ├─5985 php-fpm: pool www
           ├─5986 php-fpm: pool www
           ├─5987 php-fpm: pool www
           ├─5988 php-fpm: pool www
           ├─5989 php-fpm: pool www
           ├─5990 php-fpm: pool www
           ├─5991 php-fpm: pool www
           ├─5992 php-fpm: pool www
           ├─5993 php-fpm: pool www
           ├─5994 php-fpm: pool www
           ├─5995 php-fpm: pool www
           ├─5996 php-fpm: pool www
           ├─5997 php-fpm: pool www
           ├─5998 php-fpm: pool www
           ├─5999 php-fpm: pool www
           ├─6000 php-fpm: pool www
           ├─6001 php-fpm: pool www
           ├─6002 php-fpm: pool www
           ├─6003 php-fpm: pool www
           ├─6004 php-fpm: pool www
           ├─6005 php-fpm: pool www
           ├─6006 php-fpm: pool www
           ├─6007 php-fpm: pool www
           ├─6008 php-fpm: pool www
           ├─6009 php-fpm: pool www
           ├─6010 php-fpm: pool www
           ├─6011 php-fpm: pool www
           ├─6012 php-fpm: pool www
           ├─6013 php-fpm: pool www
           ├─6014 php-fpm: pool www
           ├─6015 php-fpm: pool www
           ├─6016 php-fpm: pool www
           ├─6017 php-fpm: pool www
           ├─6018 php-fpm: pool www
           ├─6019 php-fpm: pool www
           ├─6020 php-fpm: pool www
           ├─6021 php-fpm: pool www
           ├─6022 php-fpm: pool www
           ├─6023 php-fpm: pool www
           ├─6024 php-fpm: pool www
           ├─6025 php-fpm: pool www
           ├─6026 php-fpm: pool www
           ├─6027 php-fpm: pool www
           ├─6028 php-fpm: pool www
           ├─6029 php-fpm: pool www
           ├─6030 php-fpm: pool www
           ├─6031 php-fpm: pool www
           ├─6032 php-fpm: pool www
           ├─6033 php-fpm: pool www
           ├─6034 php-fpm: pool www
           ├─6035 php-fpm: pool www
           ├─6036 php-fpm: pool www
           ├─6037 php-fpm: pool www
           ├─6038 php-fpm: pool www
           ├─6039 php-fpm: pool www
           ├─6040 php-fpm: pool www
           ├─6041 php-fpm: pool www
           ├─6042 php-fpm: pool www
           ├─6043 php-fpm: pool www
           ├─6044 php-fpm: pool www
           ├─6045 php-fpm: pool www
           ├─6046 php-fpm: pool www
           ├─6047 php-fpm: pool www
           ├─6048 php-fpm: pool www
           ├─6049 php-fpm: pool www
           ├─6050 php-fpm: pool www
           ├─6051 php-fpm: pool www
           ├─6052 php-fpm: pool www
           ├─6053 php-fpm: pool www
           ├─6054 php-fpm: pool www
           ├─6055 php-fpm: pool www
           ├─6056 php-fpm: pool www
           ├─6057 php-fpm: pool www
           ├─6058 php-fpm: pool www
           ├─6059 php-fpm: pool www
           ├─6060 php-fpm: pool www
           ├─6061 php-fpm: pool www
           ├─6062 php-fpm: pool www
           ├─6063 php-fpm: pool www
           ├─6064 php-fpm: pool www
           ├─6065 php-fpm: pool www
           ├─6066 php-fpm: pool www
           ├─6067 php-fpm: pool www
           ├─6068 php-fpm: pool www
           ├─6069 php-fpm: pool www
           ├─6070 php-fpm: pool www
           ├─6071 php-fpm: pool www
           ├─6072 php-fpm: pool www
           ├─6073 php-fpm: pool www
           ├─6074 php-fpm: pool www
           ├─6075 php-fpm: pool www
           ├─6076 php-fpm: pool www
           ├─6077 php-fpm: pool www
           ├─6078 php-fpm: pool www
           ├─6079 php-fpm: pool www
           ├─6080 php-fpm: pool www
           ├─6081 php-fpm: pool www
           ├─6082 php-fpm: pool www
           ├─6083 php-fpm: pool www
           ├─6084 php-fpm: pool www
           ├─6085 php-fpm: pool www
           ├─6086 php-fpm: pool www
           ├─6087 php-fpm: pool www
           ├─6088 php-fpm: pool www
           ├─6089 php-fpm: pool www
           ├─6090 php-fpm: pool www
           ├─6091 php-fpm: pool www
           ├─6092 php-fpm: pool www
           ├─6093 php-fpm: pool www
           ├─6094 php-fpm: pool www
           ├─6095 php-fpm: pool www
           ├─6096 php-fpm: pool www
           ├─6097 php-fpm: pool www
           └─6098 php-fpm: pool www

Oct 27 13:38:29 web2 systemd[1]: Starting The PHP 7.0 FastCGI Process Manager...
Oct 27 13:38:29 web2 systemd[1]: Started The PHP 7.0 FastCGI Process Manager.

我真的不需要CGroup它输出的所有信息。我只想要快速状态和底部的几行日志。我可以像这样过滤输出:

root@web2:/etc/php/7.0/fpm/pool.d# systemctl --no-pager status php7.0-fpm | grep -v "php-fpm: pool www"
● php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2016-10-27 13:38:29 CDT; 19min ago
  Process: 5961 ExecStartPre=/usr/lib/php/php7.0-fpm-checkconf (code=exited, status=0/SUCCESS)
 Main PID: 5971 (php-fpm7.0)
   Status: "Processes active: 0, idle: 125, Requests: 16, slow: 0, Traffic: 0req/sec"
    Tasks: 126
   Memory: 295.5M
      CPU: 737ms
   CGroup: /system.slice/php7.0-fpm.service
           ├─5971 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)                      

Oct 27 13:38:29 web2 systemd[1]: Starting The PHP 7.0 FastCGI Process Manager...
Oct 27 13:38:29 web2 systemd[1]: Started The PHP 7.0 FastCGI Process Manager.

但是我失去了 bash 中的彩色格式,而这对于快速状态检查也非常有用。

有没有办法systemctl或类似的命令来获取此状态信息而无需CGroup

答案1

我认为,如果您以任何方式处理输出,就不可能获得颜色。systemctl检测其输出是否不是 tty 并且不产生彩色输出。这通常是默认行为,因为这通常是您想要的。

作为一种解决方法,这里有一个小脚本,可以解析 CGroup 信息并重新添加颜色:

perl -MTerm::ANSIColor -ne 'next if /^CGroup/ || /├─/; 
    s/(?>: )active/color("bold green").$&.color("reset")/e; print'

将您的systemctl输出通过该命令,单词active将变成绿色:

systemctl --no-pager status php7.0-fpm | 
   perl -MTerm::ANSIColor -ne 'next if /^CGroup/ || /├─/; 
    s/(?>: )active/color("bold green").$&.color("reset")/e; print'

如果您编辑您的问题并添加不同的输出(我没有安装服务,所以我不知道还应该为什么着色),我可以将其扩展到其他可能的输出。

答案2

使用脚本

例如script --running -c 'systemctl --no-pager status php7.0-fpm' | grep -v "php-fpm: pool www"

信用:https://stackoverflow.com/questions/1401002/trick-an-application-into-thinking-its-stdout-is-a-terminal-not-a-pipe

相关内容