Shell script to stop a java program

Harish picture Harish · Jan 25, 2010 · Viewed 10.2k times · Source

Is there a way to stop a java program running using a shell script by knowing the name alone.I am using ksh shell

Answer

sfussenegger picture sfussenegger · Jan 25, 2010

following up on Mnementh' suggestion:

this should do the job

jps -l | grep org.example.MyMain | cut -d ' ' -f 1 | xargs -rn1 kill
  • jps -l: list java process with "full package name for the application's main class or the full path name to the application's JAR file."

  • grep: choose the process you like

  • cut -d -' ' -f 1: split the output in columns using delimiter ' ' and print only the first one (the pid)

  • xargs -rn1 kill: execute kill for each PID (if any)

note that you must run jps and xargs with the same user (or root) as you're running the process