简体   繁体   中英

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.

Control Meaning (Perform operation on 1st application initiated from 2nd app ):

  • START
  • STOP
  • RESTART

My 1st Application runs on static port 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

How can i get PID of application which is running on Port (10001) ?

Code :- eSocketServer.jar is my 1st application jar file

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}' 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");

Inside your first app , just get the process id using the following statement which will return you the 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. STOP can now be implemented using the kill command in linux ( Run it the same way you ran the java -jar commmand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM