-1

I am trying to create a simple bash script that will kill a specific java process. JPS seems to be the most suitable candidate so I quickly wrote this:

jps | grep my_process_name | awk '{print $1}' 

In the terminal this works great and I get back the PID of the java process my_process_name. However when I put that into a quick script like this:

stop_app() {
    echo 'Stopping running service...'
    PID=jps | grep halo | awk '{print $1}'
    kill -9 ${PID}
}

My PID seems to be empty! Any ideas?

tarka
  • 5,289
  • 10
  • 51
  • 75
  • Use command substitution to store the output of a command to a variable: `PID=$(jps | grep halo | awk '{print $1}')` – user000001 Dec 28 '15 at 11:51
  • Note this suffices: `PID=$(jps | awk '/halo/{print $1}')`. No need to use `grep` in between. – fedorqui Dec 28 '15 at 12:25

1 Answers1

1

PID="$(jps | grep halo | awk '{print $1}')" ?

dimid
  • 7,285
  • 1
  • 46
  • 85