Runtime.getRuntime()。exec()无限运行

时间:2019-07-15 09:00:37

标签: linux multithreading spring-boot java-8 process

我试图通过位于同一服务器上的WAR在Linux上执行jar。 JAR已执行并且结果在那里,但是即使使用WaitFor和Destory时该过程也永远不会结束,我必须在结束后手动终止该过程。

当我在linux上手动执行CMD时,它可以正常工作并正常结束/退出。

代码如下:

 public void ExecuteTheROPJob(String filename, Long id_zone, Long id_user) {


    String Command =
        "sudo /tmp/jdk1.8.0_161/bin/java -jar /opt/fibertool_sync/jobs/SyncROP-jar-with-dependencies.jar  "
            + "/var/fibertool_sync/sync/rop/IN/" + filename + " " + id_zone + " " + id_user;

    logger.info("***************");
    logger.info("Executing SyncROP JAR with the Linux CMD");
    logger.info(Command);
    logger.info("***************");
    logger.info("***************");


    Process p;
    try {
      p = Runtime.getRuntime().exec(Command);

      logger.info("p.waitfor()  Before the TRY value :" + p.waitFor());

      try {

        // p.waitFor(7, TimeUnit.MINUTES);
        int exitCode = p.waitFor();
        logger.info("p.waitfor() after the TRY value :" + exitCode);


        if (exitCode == 0) {
          System.out.println(Command + " Executed successfully");
          logger.info(Command + " Executed successfully");
          p.destroy();
          logger.info("Destroy");
        } else {
          System.out.println(Command + " Failed ...");
          logger.info(Command + " Failed ...");
          p.destroy();
          logger.info("Destroy");

        }


      } catch (Exception e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
      } finally {

        if (p.isAlive()) {
          p.destroyForcibly();
          logger.info("Destroy Forcibly");
        }

      }

    } catch (IOException | InterruptedException e1) {

      e1.printStackTrace();
    }

  }


这是日志文件:

2019-07-15 10:56:30.938  INFO 16430 --- [http-nio-8080-exec-31] c.S.SyncFibertTool.Aspect.AspectLogging  : BEGIN : [ExecuteTheROPJob]
2019-07-15 10:56:30.938  INFO 16430 --- [http-nio-8080-exec-31] c.S.SyncFibertTool.Aspect.AspectLogging  : ******
2019-07-15 10:56:30.938  INFO 16430 --- [http-nio-8080-exec-31] c.S.S.Controller.FileController          : ***************
2019-07-15 10:56:30.938  INFO 16430 --- [http-nio-8080-exec-31] c.S.S.Controller.FileController          : Executing SyncROP JAR with the Linux CMD
2019-07-15 10:56:30.938  INFO 16430 --- [http-nio-8080-exec-31] c.S.S.Controller.FileController          : sudo /tmp/jdk1.8.0_161/bin/java -jar /opt/fibertool_sync/jobs/SyncROP-jar-with-dependencies.jar  /var/fibertool_sync/sync/rop/IN/ROP_HTHD_8-5.csv 1 1027
2019-07-15 10:56:30.938  INFO 16430 --- [http-nio-8080-exec-31] c.S.S.Controller.FileController          : ***************
2019-07-15 10:56:30.938  INFO 16430 --- [http-nio-8080-exec-31] c.S.S.Controller.FileController          : ***************

该过程停留在以下代码行中:

  p = Runtime.getRuntime().exec(Command);

0 个答案:

没有答案