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.

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

share|improve this question

2 Answers 2

@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
share|improve this answer
    
+1. nice. –  Prince John Wesley Jun 29 '11 at 6:01

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

share|improve this answer
    
Doesnt seem to work process still exists with both -9 and none –  Matt Freeman - nonuby Jun 29 '11 at 5:47
1  
You need to add -f to search the full command with arguments, not just the command name. –  glenn jackman Jun 29 '11 at 11:13
    
@glenn: yes. i missed that one.+1. –  Prince John Wesley Jun 29 '11 at 11:15

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.