0

I'm trying to kill a detatched screen session. Recently I have started a screen session with the following java code:

  1. Just created a new ProcessBuilder:

    ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "screen -mdS " + 
    servername + " java " + "-Xms256M -Xmx" + max_ram + "M -jar server.jar 
    ").directory(tempDirectory);
    
  2. Started a new process

    try {
    
      Process p = pb.start(); // start a process
      subserver.setProcess(p); // save the process if we need it later again
      BufferedReader reader = new BufferedReader(new 
    
      InputStreamReader(p.getInputStream()));
      String line;
      while ((line = reader.readLine()) != null) {
        System.out.println(line);
      }
    
    } catch (Exception e) {         
       e.printStackTrace();
    }   
    

But the question is: How to kill this new process ?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Go through javadoc of Process Class https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html It has destroy() method which can be used to kill the process. – Pranav Maniar Apr 04 '18 at 17:46
  • The destroy() method doesnt work for me. Furthermore the isAlive() method of the process always return false. But the screen is still running. Is there any other way? – Pauligauli Apr 04 '18 at 17:49
  • No need for `sh -c`. Just execute screen directly: `new ProcessBuilder("screen", "-mdS", servername, "java", "-Xms256M", "-Xmx" + max_ram + "M", "-jar", "server.jar")`. Also, you can do away with your BufferedReader completely and instead call `pb.inheritIO();`, which will cause the process’s standard output to appear in your calling program’s standard output. – VGR Apr 04 '18 at 18:17

0 Answers0