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.
I cannot explain what exactly fails in your example (so I admit it is some kind of voodoo programming on my side), but this is a fix that (almost -- see below*) works in my bash (Debian):
echo
;$2
.Result:
echo password | su -c "ps aux | grep verySpecificChain | grep -v grep | /usr/bin/awk '{ print \$2 }' | xargs kill" userName;
*I wrote "almost works" because Debian won't let me use su
in a pipe to echo the password. I must run it without initial echo
and type the password interactively. I guess it's not an issue in OP's Fedora.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다