Question

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?

Was it helpful?

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

OTHER TIPS

How about storing the piped commands into a small bash shell that takes a positional parameter? This would reduce keystrokes if that is the goal.

[mybox:~ #] cat mycmd
    #!/bin/bash
    ps aux | grep $1 | grep -v grep

Which could be called:

[mybox:~ #] mycmd myservice
  root      2273  0.0  0.0   4360   760 ?        Ss   18:06   0:00 /usr/sbin/myservice

ps + pgrep maybe?

$ ps -fp $(pgrep myservice)
UID    PID  PPID  C STIME TTY  TIME      CMD
user   5422  5315  0 15:37 ?   00:00:04  /usr/bin/myservice
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top