1
votes

Im trying to write a bash script and trying to take input from user and executing a kill command to stop a specific tomcat.

...

read user_input
if [ "$user_input" = "2" ]
then
ps -ef | grep "search-tomcat" |awk {'"'"'print $2'"'"'}| xargs kill -9
echo "Search Tomcat Shut Down"
fi

...

I have confirmed that the line

ps -ef | grep "search-tomcat"

works fine in script but:

ps -ef | grep "search-tomcat" |awk {'"'"'print $2'"'"'}

doesnt yield any results in script, but gives desired output in terminal, so there has to be some problem with awk command

1
you can try awk '{print "\047" $2 "\047"}' ? - Jose Ricardo Bustos M.
That seems to work! But now Im stuck at the last part: passing the results as a parameters to xargs. - Bhuvan Rawal
@BhuvanRawal: Do you have pkill command available? - anubhava
Then you just need pkill -9 -f "search-tomcat" - anubhava
use the {} tool in the edit box menu at the top left on highlighted text get get properly formatted code, data, output and error msgs ;-) Good luck! - shellter

1 Answers

1
votes

xargs can be tricky - Try:

kill -9 $(ps -ef | awk '/search-tomcat/ {print $2}')

If you prefer using xargs then check man page for options for your target OS (i.e. xargs -n.)

Also noting that 'kill -9' is a non-graceful process exit mechanism (i.e. possible file corruption, other strangeness) so I suggest only using as a last resort...

:)