2

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?

4 Answers 4

3

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
1
  • I was hoping for a more elegant solution, but this clearly works.
    – Chris R
    May 25, 2011 at 16:21
2

The trick is to trick grep into not finding itself.

... | grep "[s]sh" | ...
1

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.

0
1

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

You must log in to answer this question.

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