[英]stop/start/restart java application from another java application
I have two java application running on my linux(centos)
system.first application done the processing and 2nd application CONTROLS 1st application. 我在
linux(centos)
系统上运行了两个Java应用程序。第一个应用程序完成了处理,第二个应用程序CONTROLS第一个应用程序。
Control Meaning (Perform operation on 1st application initiated from 2nd app ): 控制含义(对从第二个应用启动的第一个应用执行操作):
My 1st Application runs on static port 10001
我的第一个应用程序在静态端口
10001
上运行
How can i perform these operation from my second application ? 如何从第二个应用程序执行这些操作? I found this articles which get me somewhat closer.
我发现这些文章使我更加接近。
https://stackoverflow.com/a/14340719/1169180 & https://stackoverflow.com/a/138098/1169180 https://stackoverflow.com/a/14340719/1169180和https://stackoverflow.com/a/138098/1169180
How can i get PID
of application which is running on Port (10001)
? 如何获取在端口
(10001)
上运行的应用程序的PID
?
Code :- eSocketServer.jar is my 1st application jar file 代码:-eSocketServer.jar是我的第一个应用程序jar文件
Process p = Runtime.getRuntime().exec(
"ps -ef | grep eSocketServer.jar | awk '{print $2}'");
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((br.readLine()) != null)
System.out.println("line: " + br.readLine());
p.waitFor();
System.out.println("exit: " + p.exitValue());
p.destroy();
It doesnt giving me any output by when same command is executed by shell prompt ps -ef | grep eSocketServer.jar | awk '{print $2}'
当shell提示符
ps -ef | grep eSocketServer.jar | awk '{print $2}'
执行相同的命令时,它没有给我任何输出 ps -ef | grep eSocketServer.jar | awk '{print $2}'
You can create a runnable jar
of the program and START it from the controller using: Runtime.getRuntime().exec("java -jar JAR-PATHNAME");
您可以创建程序的
runnable jar
并使用以下命令从控制器启动它: Runtime.getRuntime().exec("java -jar JAR-PATHNAME");
Inside your first app , just get the process id
using the following statement which will return you the PID
. 在您的第一个应用程序中,只需使用以下语句获取
process id
,这将返回PID
。 ManagementFactory.getRuntimeMXBean().getName().substring(0, ManagementFactory.getRuntimeMXBean().getName().indexOf("@")
Write this PID
to a file and have the controller
read it from the file. 将此
PID
写入文件,并让controller
从文件中读取它。 STOP
can now be implemented using the kill
command in linux ( Run it the same way you ran the java -jar
commmand. 现在可以在Linux中使用
kill
命令来实现STOP
(以与运行java -jar
commmand相同的方式运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.