0

If I do service --status-all I get something like this:

[ ? ]  webmin
[ ? ]  whoopsie
[ + ]  winbind
[ - ]  x11-common

I want to show just the running ones ([ + ]) so I did

service --status-all | grep +

which highlights them, but how do I make it exclude the other lines?

Also, is this showing actual running services or just ones which are scheduled to run at startup?

1 Answer 1

1

You can change the amount of surrounding lines grep outputs (called the "context") with options -C [num] or --context=[num].

Usually, the default option for grep is to print no context, so the command you used should be fine. You can force it to show only matching lines by service --status-all | grep + -C 0.

To exclude matching lines, use -v or --invert-match. So, you can pipe the original command through | grep -v '[ ?' | grep -v '[ -' to get rid of lines with - or ? as their status. You can also combine multiple match strings by using escaped "or" (the pipe symbol), like grep -v '[ ?\|[ -'.

However, as service for some reason directs its output to stderr instead of the normal stdout, the output streams need to be combined with |& for the grep to work properly. So, the working command here would be service --status-all |& grep +.

2
  • service --status-all | grep + -C 0 does exactly the same thing. Using grep -v also doesn't work. However, this works: ` service --status-all > services.txt cat services.txt | grep +`
    – eggbert
    Commented Aug 14, 2013 at 15:17
  • Ok, service behaves erratically, please see the last paragraph of the edited answer.
    – Jawa
    Commented Aug 14, 2013 at 19:22

You must log in to answer this question.

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