[英]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.