1

So, I want to highlight the status of services on a dynamic motd. I am currently using the following command for php-fpm:

service php5-fpm status|grep Active|cut -d':' -f2-

I've tried several solutions to achieve this. Th two followings are doing great job to detect 1/ when everything works fine 2/ all the others cases.

service php5-fpm status|grep Active|cut -d':' -f2-|GREP_COLOR='1;32' grep --color=always " active \(.*\)"

service php5-fpm status|grep Active|cut -d':' -f2-|GREP_COLOR='1;31' grep --color=always -E ".* \(.*\)"

What I have tried to do, is to use the || to have them in the same command. This is working fine when the first grep return 0, but when it fails and return 1, the second grep seems to not work.

service php5-fpm status|grep Active|cut -d':' -f2-|(GREP_COLOR='1;32' grep --color=always -E " active \(.*\)" || GREP_COLOR='1;31' grep --color=always -E ".* \(.*\)")

When runed with bash -x I am getting the following output:

+ GREP_COLOR='1;32'
+ grep --color=always -E ' active \(.*\)'
+ cut -d: -f2-
+ grep Active
+ service php5-fpm status
+ GREP_COLOR='1;31'
+ grep --color=always -E '.* \(.*\)'

So ... I have no idea right now, and I'm hoping someone will see where I'm doing something wrong.

  • @don_crissti Tottaly true, but all the informations I need are : is this service UP, and since when. The verbosity of systemctl status is not what I wanted for my MOTD. – V.Frenot Mar 23 '17 at 13:58
2
Where will the 2nd grep get it's input from when the 1st grep fails?
Coz, grep1 consumes all the stdin with nothing left for grep2.In the
case of grep1 succeeding, grep2 never runs so is not an issue.

We may rig it up like the following to achieve what you want:

#/bin/sh
service php5-fpm status |
grep Active |
cut -d':' -f2- | tee /tmp/log |
GREP_COLOR='1;32' grep --color=always -E " active \(.*\)" - ||
GREP_COLOR='1;31' grep --color=always -E ".* \(.*\)") /tmp/log
| improve this answer | |
  • I was trying an other solution just after reading your first comment. status=service php5-fpm status|grep Active|cut -d':' -f2- echo $status | GREP_COLOR='1;32' grep --color=always -E "^active (.*)" || echo $status | GREP_COLOR='1;31' grep --color=always -E ".* (.*)" – V.Frenot Mar 23 '17 at 13:41
0

Ok, so, here's my own solution, based on Rakesh Sharma comment to my question, as I did not wanted to create temporary file.

function service_status() {
    status=`service $1 status | grep Active | cut -d':' -f2-`
    echo "$status" | GREP_COLOR='1;32' grep --color=always -E "^ active \(.*\)" || \
    echo "$status" | GREP_COLOR='1;31' grep --color=always -E ".* \(.*\)"
}
| improve this answer | |
  • Can I suggest some indentation to the lines inside the function, and perhaps a newline after the ||? Also: put quotes around $status. – ilkkachu Mar 23 '17 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.