Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 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
share|improve this answer
I was hoping for a more elegant solution, but this clearly works. – Chris R May 25 '11 at 16:21

The trick is to trick grep into not finding itself.

... | grep "[s]sh" | ...
share|improve this answer

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.

share|improve this answer

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!)

share|improve this answer

You must log in to answer this question.

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