Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script to stop a java program

Tags:

java

shell

ksh

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

like image 869
Harish Avatar asked Jan 25 '10 11:01

Harish


People also ask

How do you quit a program in shell?

Ctrl+C sends the SIGINT signal to gedit . This is a stop signal whose default action is to terminate the process. It instructs the shell to stop gedit and return to the main loop, and you'll get the prompt back.

How do you stop a Java application?

Firstly, we can use the exit() method of the System class, honestly, it is the most popular way to stop a program in Java. System. exit() terminates the Java Virtual Machine(JVM) that exits the current program that we are running.

How do I stop a Java program from terminal?

It is possible to force java application to terminate immediately with simple method call: System. exit(0); This method can be called anytime from within the code (eg.

How do I stop Java from running on Linux?

Use jps to list running java processes. The command returns the process id along with the main class. You can use kill command to kill the process with the returned id or use following one liner script.


1 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 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

like image 166
sfussenegger Avatar answered Oct 11 '22 09:10

sfussenegger