Ubuntu – The “service –status-all | grep thesql” get many unrelated result

command linegrepservices

It seems weird here while running

service --status-all | grep mysql

why I get so many wrong result?

It works fine on Centos where i am getting:

mysql is running xxxxxxx

This is what i am getting on Ubuntu:

enter image description here

Best Answer

service --status-all shows its output on both STDOUT (file descriptor 1) and STDERR (file descriptor 2), with the services for which the status can be determined on STDOUT (indicated by [+] or [-]) and the services whose status can not be determined on STDERR (indicated by [?]).

While doing service --status-all | grep mysql, you are running grep over only the STDOUT of service --status-all, that's why everything showing on STDERR are being shown (along with the possible output of grep).

To get only the desired result, redirect both run redirect both STDOUT and STDERR to grep:

service --status-all |& grep mysql

Test:

Look at the signs inside []:

% service --status-all >/dev/null  ## Discarding STDOUT
 [ ? ]  apport
 [ ? ]  binfmt-support
 [ ? ]  console-setup
 [ ? ]  dns-clean
 [ ? ]  irqbalance
 [ ? ]  killprocs
 [ ? ]  kmod
 [ ? ]  lightdm
 [ ? ]  mysql


% service --status-all 2>/dev/null  ## Discarding STDERR
 [ + ]  acpid
 [ - ]  anacron
 [ - ]  apparmor
 [ + ]  atd
 [ + ]  atop
 [ + ]  avahi-daemon
 [ + ]  bluetooth
 [ - ]  brltty
 [ + ]  cron