Is there a way to stop a java program running using a shell script by knowing the name alone.I am using ksh 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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With