2

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

1 Answer 1

5

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
4
  • 1
    Amazing Answer tnx a lot .
    – lizhe
    Jan 12, 2016 at 10:12
  • One more question, how do you know service --status-all shows its output on both STDOUT (file descriptor 1) and STDERR (file descriptor 2). I can't get a clue in the manual (man service)
    – lizhe
    Jan 12, 2016 at 10:15
  • @lizhe Yeah, the man page does not contain this..you need to either check it yourself or check the source code..it's kind of intuitive you see :)
    – heemayl
    Jan 12, 2016 at 10:18
  • You help me a lot, Tnx. :)
    – lizhe
    Jan 12, 2016 at 10:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .