Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

4 Answers

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
add comment

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
add comment

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
add comment

The trick is to trick grep into not finding itself.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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