I'm using Mac OS X, which means I don't have (for example) pgrep/pkill, but I want something like that (I don't use macports/fink, either - homebrew is my Unix package tool of choice - so packages from those won't do).

I want to find and kill an instance of ssh with particular parameters - basically, a particular remote ssh port forwarding tunnel. The command I'd use is something like this:

ps aux | grep ssh | grep remote-host | grep port-forwarding-stmt

However, sometimes this grep command actually shows up in the output, so the current command string is not useful as written.

Is there a tool for Mac OS X that I can use to do this?

link|improve this question

33% accept rate
feedback

4 Answers

up vote 2 down vote accepted

Add an additional grep to the string but use the invert option to remove all grep output requests:

ps aux | grep ssh | grep remote-host | grep port-forwarding-stmt | grep -v grep
link|improve this answer
I was hoping for a more elegant solution, but this clearly works. – Chris R May 25 '11 at 16:21
feedback

The trick is to trick grep into not finding itself.

... | grep "[s]sh" | ...
link|improve this answer
feedback

Can also use awk:

ps axuwww | awk '!/grep/ && /ssh/ && /remote-host/ && /port-forwarding-stmt/'

The 'www' arguments to ps(1) tell to display the full command-line, instead of a shortened version.

link|improve this answer
feedback

ps -a -o pid, command | awk '(/[s]sh/ && /remote-host/ && /port-forwarding-stmt/){ print $1}'

combines some of the stated ideas. The -o part specifies that we only print pid and commandline, and print $1 only prints the pid part. The [s]sh makes it not find itself (nice trick!)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.