在java中创建分离的子进程

时间:2014-09-12 07:29:21

标签: java linux shell

我正在尝试更新正在运行的java程序,为此我正在执行shell脚本来执行更新。 这个如何在剧本中午餐:

Runtime.getRuntime().exec("chmod +x " + spherDir + "/deploy.sh");

问题是在deploy.sh中有一个命令在脚本完成执行之前终止正在运行的实例。

我的问题是如何创建一个分离的进程,即使主程序(父程序)停止也会继续执行!!

我在linux上,我已经尝试ProcessBuilder但没有成功。

这里是java代码:

File file = new File(spherDir +"/restore.sh");
boolean bool = file.createNewFile();
FileWriter writer = new FileWriter(file);

try
{
    writer.write("#!/bin/sh\n\n");
    writer.write("service spher stop \n"); // stop the server chameleon
    writer.write("rm -rf " + appServerDir + " \n");
    writer.write("cp -Rap " + spherDir + "/currentdistrib/app-server  " + spherDir +"/app-server \n");
    writer.write("rm -rf " + spherDir + "/ui-server \n");
    writer.write("cp -Rap " + spherDir + "/currentdistrib/ui-server  " + spherDir + "/ui-server \n");
    writer.write("rm -rf " + spherDir + "/currentdistrib \n");
    writer.write("chmod +x " + spherDir + "/app-server/chameleon.sh \n"); /// Alaways do that to ensure that the script is runnable
    writer.write("sleep 5s \n"); /// Alaways do that to ensure that the script is runnable
    writer.write("touch README \n"); /// Alaways do that to ensure that the script is runnable
    writer.write("service spher start \n");

} catch(IOException ex)
{
    ex.printStackTrace();
}
finally
{
    writer.close();
}

Runtime.getRuntime().exec("chmod 755 " + spherDir + "/restore.sh");
ProcessBuilder pb = new ProcessBuilder("/bin/bash", spherDir + "/restore.sh");
final Process process = pb.start();

0 个答案:

没有答案