Shorter output from systemctl status

1

1

Is there a way to get shorter/custom output from systemctl status? I really only need the active line prepended with the name of the service. So something like this:

        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

only with color feedback. But I would settle for it without color. Or on two lines plus a blank line like:

● 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

This kind of satisfies the latter btw:

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

But it messes up the colors doesn't have the whitespace and I kind of expect there is a systemctl option or configuration somewhere where I can get exactly what I want.

Kaan

Posted 2020-02-06T15:06:45.933

Reputation: 111

Answers

1

So there's no native way in systemctl to format the output in this way, but we can do it anyways by getting a bit creative with pipes.

First, we need a list of all running services on the system:

# 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 
...

No time output. To get that we have to invoke systemctl show against the unit with the --property=ActiveEnterTimestamp flag. Example:

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

Now if only we had a way to staple that output onto the end of the systemctl output.. we do!

This is an ugly one-liner, but it gets the job done:

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`

To explain:

  • The cut command is splitting on spaces and taking the first field from systemctl, which is the service name.
  • We enter a while loop that pipes that service name into a variable named $f
  • We create a variable named STARTTIME that contains the systemctl show output with the start time flag.
  • We use sed to strip the actual ActiveEnterTimestamp= text, giving us the time only.
  • Finally, we echo the service name and the cleaned up start time, separated with a space.

The final output looks like this:

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

Mikey T.K.

Posted 2020-02-06T15:06:45.933

Reputation: 3 224