tanmay2507 tanmay2507 - 1 year ago 88
Bash Question

Getting unexpected newline error while doing awk

I am running the following script in linux command line:

ps -ef| grep "java -Droute=full" | awk 'NR!=1{print $2}' | while read output;
pid=$(echo $output)
kill $pid

I am getting the following error:

awk: cmd. line:1: NR!=1{print
awk: cmd. line:1: ^ unexpected newline or end of string

I believe the quotes and spaces are consistent, but why am i still getting this error?

Output of
ps -ef | grep "java -Droute=full"

root 24441 1 0 Apr22 ? 00:00:00 sudo nohup java -Droute=full -jar /opt/corona/artifacts/imn_integration/imn_item_publisher-1.0.10.jar --spring.config.location=/etc/corona/application_full.properties
root 24442 24441 7 Apr22 ? 06:41:10 java -Droute=full -jar /opt/corona/artifacts/imn_integration/imn_item_publisher-1.0.10.jar --spring.config.location=/etc/corona/application_full.properties
tanmayg 30804 30771 0 05:27 pts/0 00:00:00 grep --color=auto java -Droute=full

Answer Source

The actual problem you're trying to solve is something like "How to kill the process whose command line contains java -Droute=full.

A much easier, safer way is to just use pkill:

pkill -f 'java -Droute=full'

This replaces your entire script.