Thursday, February 22, 2007

A more targeted kill

A few posts ago, we learnt how to kill by name (killall "name"). We also know how "xargs" works. (See previous posts if you don't know to use either)

While this works for a number of cases, we will often have the case where the actual command might be the same, but what matters is the path it is invoked from or some such stuff.

For example, I have the command "test" in two directories:

/home/me/scripts/test
/home/me/tools/test

I want to kill only those "test" commands which are from the "tools" directory. A little bit of awking is required. My command in this case will be:

ps aux | grep 'tools/test' | gawk '{print $2}' | xargs kill -9

However, if you want to run this remotely (in another machine), you may have to invoke
bash -c " ps aux | grep 'ssh' | gawk \"{print \"\\\$2\"}\" " within your ssh command (it will lead to a case of painful backslashitis if I included the full command here, so am taking the easy way out).

No comments: