0

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}'

Community
  • 1
  • 1
Shaggy
  • 5,422
  • 28
  • 98
  • 163

1 Answers1

0

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.

Aneesh
  • 1,703
  • 3
  • 24
  • 34
  • does this same work for `start & restart ? ` and can i stop my 1st application in a more legitimate way ? i.e. `System.exit(0)` instead of `kill` – Shaggy Sep 03 '13 at 12:10
  • Depending on your exact implementation requirement , the approach may change. One I suggested is just one of the many approaches. – Aneesh Sep 03 '13 at 12:14