Search code examples
shellgrepshortcutidiomsps

What do I do instead of grep'ing the output of ps?


Everyone knows how annoying this is:

[mybox:~ #] ps aux | grep myservice
root      2273  0.0  0.0   4360   760 ?        Ss   18:06   0:00 /usr/sbin/myservice
root     18590  0.0  0.0   4100   788 pts/2    S+   21:21   0:00 grep myservice

you get the processes you were looking for, plus the grep. So, ok you could do:

[mybox:~ #] ps aux | grep myservice | grep -v grep
root      2273  0.0  0.0   4360   760 ?        Ss   18:06   0:00 /usr/sbin/myservice

or

[mybox:~ #] pgrep myservice
2273

but the former is 3 whole commands, and the latter only gets you the process id. Is there some nicer alternative?


Solution

  • If you now the full name of the process

    ps -F -C myservice
    

    But it wouldn't work with myserv of 'myserv*'

    If you know only a partial one, you can reduce your commands to 2:

    ps aux | grep [m]yserv