Skip to content
Advertisement

BASH – weird characters coming out from a pipe

I have this script which simply kills an app

KEY_NAME=/some/path
kill `ps -ef | grep $KEY_NAME | grep -v grep | awk '{ print $2 }'`
exit 0

If I write the kill command manually as a oneliner than it works. But I have to run it as a script which doesn’t work. So I tried to debug it and here is the result

[user@server logs]$ bash -x ./script.sh
+ KEY_NAME=/some/path
++ ps -ef
++ grep /some/path
++ grep -v grep
++ awk '{ print $2 }'
+ kill $'20557r'
: arguments must be process or job IDs
+ exit 0

Why do I keep getting those strange characters on the kill line? How can i get rid of them?

Advertisement

Answer

Might be the case is number of pid’s returned are more than 1 Change your line to

ps -ef | grep $KEY_NAME | grep -v grep | awk '{ print $2 }'|xargs kill
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement