codepleb
codepleb

Reputation: 10571

How to send another command to an existing process?

I got the following code that starts a minecraft server:

public class App {

    public static void main(String...args) throws Exception {
        final ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.directory(new File("C:/Users/trudler/Desktop/New folder (4)/"));
        processBuilder.command("java", "-jar", "server.jar");
        Process process = processBuilder.start();

        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;

        System.out.printf("Output of running %s is:", Arrays.toString(args));

        while ((line = br.readLine()) != null) {
          System.out.println(line);
        }
    }
}

I want to do daily backups, so I need to send a "stop" command everyday, to be sure that the files won't be touched while I do the backup (and "start" the server again afterwards).

How can I do this?

I tried it using processBuilder.command("stop"); but that doesn't seem to work.

Upvotes: 1

Views: 1144

Answers (1)

oli-ver
oli-ver

Reputation: 362

I think you want to send commands to an existing process, so I think this is what you are looking for:

Execute external program using ProcessBuilder and provide input

public class App{

   public static void main(String... args) throws Exception {
      while (true) {
         Process process = Example.startMinecraft(args);
         // Stops for sixty seconds
         Thread.sleep(1000 * 60);

         BufferedWriter out = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
         out.write("stop");

         // Wait for the process to stop
         process.waitFor();

         // Now start your Backup
         Process backupProcess = Example.startBackup();
         backupProcess.waitFor();

         // After your backup completed your minecraft server will start again
      }

   }

   private static Process startMinecraft(String... args) throws IOException {
      final ProcessBuilder processBuilder = new ProcessBuilder();
      processBuilder.directory(new File("C:/Users/trudler/Desktop/New folder (4)/"));
      processBuilder.command("java", "-jar", "server.jar");
      Process process = processBuilder.start();

      InputStream is = process.getInputStream();
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);

      Thread t1 = new Thread(() -> {
         try {
            String line;
            System.out.printf("Output of running %s is:", Arrays.toString(args));
            while ((line = br.readLine()) != null) {
               System.out.println(line);
            }
         } catch (IOException e) {
            // Do something when the Exception is thrown
         }
      });
      t1.start();

      return process;
   }

   private static Process startBackup(){
      // Here you have to build your backup process
   }
}

If you are on a linux machine I would advise to use some script in /etc/init.d/ instead and use a restart command using this script in a cron job.

Upvotes: 4

Related Questions