简体   繁体   English

JAR完成执行后如何终止Java进程

[英]How to terminate the Java process when the JAR has finished execution

I have a Java Program that does some web services call. 我有一个执行某些Web服务调用的Java程序。 i packed that program in a JAR file and placed it my linux machine. 我将该程序打包到一个JAR文件中,并将其放在我的Linux机器上。 Then i made a .sh file the content of which were 然后我做了一个.sh文件,其内容是

#!/bin/sh 
. /etc/profile 
echo "The Script Starts now!!!!!!!!!!" 

export JAVA_HOME=/u01/app/oracle/java/java64/jrockit-jdk1.6.0_29 
export PATH=$JAVA_HOME/bin:$PATH 
cd /u01/CRM_PRD/stores/CRM_COC_Utility/Jars 

java -jar CRM_AccountCOC.jar 

echo "The Script ends now!!!!!!!!!!"

This sh file i have scheduled in cronjob to run after every 45 mins which means the above command is executed after every 45 mins. 我已经在cronjob中安排了这个sh文件,每45分钟运行一次,这意味着以上命令每45分钟执行一次。

The problem is when i do the TOP command say after a day or two, I can see multiple execution state of this JAR.Due to this the %CPU usage of my linux server has increased. 问题是当我在一两天后执行TOP命令时,我可以看到此JAR的多个执行状态。由于这个原因,我的Linux服务器的%CPU使用率增加了。

The JAR takes 10 mins to finish and the frequency to reexercute the JAR is after 45 mins. JAR需要10分钟才能完成,重新执行JAR的频率是45分钟之后。 So what i need to do in my code or in sh file such that when this JAR has executed, the java process is also killed. 因此,我需要在我的代码或sh文件中执行的操作,以便在执行此JAR时也杀死了Java进程。

Hope it explained my issue.I am not much pro in linux and Java 希望它能解释我的问题。我不是Linux和Java专业人士

Basically, if all the non-daemon threads are finished, then the java application terminates. 基本上,如果所有非守护进程线程都已完成,则Java应用程序将终止。 However, you can force it to terminate by: 但是,您可以通过以下方式强制其终止:

System.exit(0);

write PID of your java process into file: 将Java进程的PID写入文件:

#!/bin/sh 
. /etc/profile 
echo "The Script Starts now!!!!!!!!!!" 

export JAVA_HOME=/u01/app/oracle/java/java64/jrockit-jdk1.6.0_29 
export PATH=$JAVA_HOME/bin:$PATH 
cd /u01/CRM_PRD/stores/CRM_COC_Utility/Jars 

java -jar CRM_AccountCOC.jar & # run jar as background process
echo $! > program.pid

echo "The Script ends now!!!!!!!!!!"

and kill it upon cron : 并在cron杀死它:

kill -9 `cat program.pid`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在java中确定使用Runtime环境创建的进程是否已完成执行? - In java determine if a process created using Runtime environment has finished execution? 如何在Java中终止进程? - How to terminate a process in java? 即使主线程已完成执行,Java 应用程序如何继续运行? - How does a Java application keeps on running even when main thread has finished execution? Java jar 执行在另一个 java 程序在进程中启动时卡住 - Java jar execution stuck when launched in a process by another java program 即使线程尚未完成运行,如何终止它? - How to terminate a thread even if it has not finished running? 如何阻止语句执行直到在Java中完成之前? - how to block a statement from execution till the previuos has finished in java? 如何在java中终止线程的执行? - How to terminate the execution of thread in java? 如何终止Java启动的进程 - How to terminate a process initiated by java 有没有办法知道我的主要Java程序启动的所有进程何时完成执行? - Is there a way to know when all the processes started by my main java program has finished execution? Android / Java-如何检查OutputStream完成写入字节的时间 - Android/Java - How check when the OutputStream has finished to write the bytes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM