systemctl status 的简短输出

systemctl status 的简短输出

有没有办法从 systemctl status 获得更短/自定义的输出?我真的只需要在活动行前面加上服务名称。所以像这样:

        apache2: active (running) since Thu 2020-02-06 17:20:42 +03; 16min ago
        mongodb: inactive (dead)  since Thu 2020-02-06 17:20:47 +03; 16min ago
rabbitmq-server: active (running) since Thu 2020-02-06 17:20:52 +03; 16min ago
 mongodb-server: active (running) since Thu 2020-02-06 17:20:54 +03; 16min ago
          mysql: active (running) since Thu 2020-02-06 17:20:57 +03; 16min ago

只有颜色反馈。但我愿意接受没有颜色的。或者在两行加上一个空白行,例如:

● apache2.service - The Apache HTTP Server
   Active: active (running) since Thu 2020-02-06 17:20:42 +03; 16min ago

● mongodb.service - An object/document-oriented database
   Active: inactive (dead) since Thu 2020-02-06 17:20:47 +03; 16min ago

● redis-server.service - Advanced key-value store
   Active: active (running) since Thu 2020-02-06 17:20:52 +03; 16min ago

● rabbitmq-server.service - RabbitMQ Messaging Server
   Active: active (running) since Thu 2020-02-06 17:20:54 +03; 16min ago

● mysql.service - MySQL Community Server
   Active: active (running) since Thu 2020-02-06 17:20:57 +03; 16min ago

顺便说一下,这满足后者:

systemctl status apache2.service mongodb.service \
redis.service rabbitmq-server.service mysql.service | grep -e Active -e ●

但是它弄乱了颜色,没有空白,我希望有一个 systemctl 选项或配置,在那里我可以得到我想要的东西。

答案1

因此,没有本机方式可以通过systemctl这种方式格式化输出,但是我们可以通过使用管道稍微发挥一下创造力来做到这一点。

首先,我们需要系统上所有正在运行的服务的列表:

# systemctl -t service --state=running --no-legend --no-pager

accounts-daemon.service                         loaded active running Accounts Service                                               
atd.service                                     loaded active running Deferred execution scheduler                                   
containerd.service                              loaded active running containerd 
...

没有时间输出。要获得该输出,我们必须systemctl show针对带有标志的单元进行调用--property=ActiveEnterTimestamp。示例:

# systemctl show accounts-daemon.service --property=ActiveEnterTimestamp
ActiveEnterTimestamp=Wed 2019-12-11 21:44:50 UTC

现在如果我们有办法将该输出钉在输出的末尾systemctl.. 我们就有!

这是一个丑陋的单行代码,但它完成了工作:

systemctl -t service --state=running --no-legend --no-pager | cut -d ' ' -f 1 | while read f; do STARTTIME=`systemctl show $f 
--property=ActiveEnterTimestamp | sed 's/ActiveEnterTimestamp=//'`; echo "$f $STARTTIME"; done`

解释:

  • cut命令按空格分割并从中取出第一个字段systemctl,即服务名称。
  • 我们进入一个 while 循环,将该服务名称导入名为$f
  • 我们创建一个名为的变量STARTTIME,其中包含systemctl show带有开始时间标志的输出。
  • 我们用来sed剥离实际ActiveEnterTimestamp=文本,仅留下时间。
  • 最后,我们将echo服务名称和清理的启动时间用空格分隔。

最终输出如下所示:

accounts-daemon.service Wed 2019-12-11 21:44:50 UTC
atd.service Wed 2019-12-11 21:44:48 UTC
containerd.service Wed 2019-12-11 21:44:50 UTC

相关内容