How to run a shell script from Java and have it continue running after JVM shutdown?

前端 未结 1 1463
忘掉有多难
忘掉有多难 2021-01-23 07:56

I\'m writing a plugin in order to restart a server application on Linux (though I\'m testing on OSX). The way I\'m doing this is using a shell script which commands the applicat

相关标签:
1条回答
  • 2021-01-23 08:35

    When you run a process from java you are creating a shell instance which then runs the process. The shell will only exit once this process has finished even if it is being run in the background &

    To run a process in headless mode you need to use the nohup command. For details, see here.

    A usage could look like this:

    ProcessBuilder processBuilder = new ProcessBuilder("nohup", "sh", "restart.sh");
    try {
        processBuilder.directory(new File(System.getProperty("user.dir")));
        processBuilder.redirectErrorStream(false);
        processBuilder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题