Ubuntu: find service using grep



Question:

I am trying to find specific service and I have question about this command:

service --status-all | grep worker  

Why did service --status-all | grep ... not work?


Solution:1

There could be one of 2 issues here.

  1. Either you have no process called worker - I didn't. Try to grep for kill or dm - you should see something for them.

  2. You are seeing all of them. This is because it's giving it as lots of individual outputs, not just 1 thing.

    Each of those lines is printed separately, and it's as stderr not stdout. You have 2 options.

    1. Redirect to a file, like this:

      service --status-all > allout.txt 2>&1  

      All the output will be saved in a file called allout.txt. You can then cat and grep that:

      cat allout.txt | grep worker  

      That can be combined to one command:

      service --status-all > allout.txt 2>&1 && cat allout.txt | grep worker  
    2. Redirect stderr to /dev/null, and grep stdout:

      service --status-all 2>&1 > /dev/null | grep worker  

    Both of these give the same output:

    tim@Hairy14:~$ service --status-all > allout.txt 2>&1 && cat allout.txt | grep kill   [ ? ]  killprocs  

    and

    tim@Hairy14:~$ service --status-all 2>&1 > /dev/null | grep kill   [ ? ]  killprocs  


Solution:2

You can easily use the hack double Tab

once you run :

sudo service wo   

then double Tab this would automcplete or list all services startting with wo


you can still use grep if your terminal support colors

for example here is a test for my services:

 service --status-all | grep vnstat  

enter image description here


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »