1

As part of tunnel script I wan to remove/kill any existing tunnels

So far I managed to get to:

matt@HY731AA:~$ ps aux | grep "ssh -N lin1tunnels" | grep -v grep | awk '{print $2}' | xargs -0 kill -9 ERROR: garbage process ID "5321 5322 ".

It looks as if the output contains " although removing the xargs does not show ", is there a way to clean up output ready for xargs. Or maybe someone knows a better way to accomplish the above

2 Answers 2

4

@John's suggestion of pkill is the cleaner way of doing this (if you have it), but just for the record: the problem is that you're using xarg's -0 flag (which tells it to look for null-delimited values), but sending newline-delimited values. Also, rather than using a second grep to remove the first one from your results, search for a pattern that won't match itself:

ps aux | grep "[s]sh -N lin1tunnels" | awk '{print $2}' | xargs kill -9
0
4

Edit:

Use pkill:

 pkill -9 -f "ssh -N lin1tunnels"

Avoid sending SIGKILL signal to the specified process or process group. Read the Useless Use of Kill -9

3
  • Doesnt seem to work process still exists with both -9 and none Jun 29, 2011 at 5:47
  • 1
    You need to add -f to search the full command with arguments, not just the command name. Jun 29, 2011 at 11:13
  • @glenn: yes. i missed that one.+1. Jun 29, 2011 at 11:15

You must log in to answer this question.

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