Is there a way to stop a java program running using a shell script by knowing the name alone.I am using ksh shell
5 Answers
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 likecut -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
2 Comments
kill to use.-r (--no-run-if-empty) to xargs to avoid this error.Add a unique property to the JVM to identify it easily, e.g. for test.class
java -Duniquename=1 test
To kill it:
ps ax | grep uniquename | grep -v grep | awk '{print $1}' | xargs kill
You can use jps identifying the process-id associated with the name of the started java-program (jps is a process-manager for java-programs). With this id you can kill the process normally.
javathough),killallis your friend.