I want to kill every processes who match a specific chain. Here is my script, which works pretty well:
echo `ps aux | grep verySpecificChain | grep -v grep | /usr/bin/awk '{ print $2 }'` | xargs kill
Now I want to execute this script from an other user via `su -c:
echo password | su -c "echo `ps aux | grep verySpecificChain | grep -v grep | /usr/bin/awk '{ print $2 }'` | xargs kill" userName;
My problem is that when grep verySpecificChain
matches several processes,
only the first element is passed to xargs kill
:
30598 ==> killed
bash: line 1: 30599: command not found ==> Not killed
bash: line 2: 30600: command not found ==> Not killed
bash: line 3: 30606: command not found ==> Not killed
I really would like to understand why with or without su -c
the command behaviour changes?
I'm running GNU bash, version 4.2.53(1)-release (x86_64-redhat-linux-gnu) on Fedora 20.
pkill
will be simpler to use than grepping the output fromps
.pgrep
andpkill
use program names only (java, awk, grep...). Assuming my verSpecifyChain is a java parameter then it does not work.-f
option did the trick, so you were right!