2013-01-05 7 views
2

완료되면 데몬을 다시 시작할 수 있어야하는 자동 업데이트 스크립트를 작성하고 있습니다.Java processbuilder 하위 프로세스가 exit 후 계속됩니다.

나는 현재이 노력하고있어 :

final ArrayList<String> command = new ArrayList<String>(); 
    String initScriptPath = Config.GetStringWithDefault("init_script", "/etc/init.d/my-daemon"); 
    command.add("/bin/bash"); 
    command.add("-c"); 
    command.add("'" + initScriptPath + " restart'"); 

    StringBuilder sb = new StringBuilder(); 
    for (String c : command) { 
     sb.append(c).append(" "); 
    } 
    Log.write(LogPriority.DEBUG, "Attempting restart with: " + sb.toString()); 

    final ProcessBuilder builder = new ProcessBuilder(command); 

    builder.start(); 

    // Wait for a couple of seconds 
    try { 
     Thread.sleep(5000); 
    } catch (Exception e) { 
    } 

    System.exit(0); 

는 그러나 System.exit와는 다시 시작을 중지하는 것? 실제로 멈추지 만 다시 시작하지는 않습니다.

final ProcessBuilder builder = new ProcessBuilder(command); 
builder.redirectErrorStream(true); 
final Process process = builder.start(); 
final int processStatus = process.waitFor(); 

을 그리고 그것은 출력 버퍼가 가득 차면 차단하는 과정의 원인이 될 수 있습니다 당신은 프로세스의 출력 스트림을 소비해야합니다

+0

여기에 질문하는 내용은 다음과 같습니다. "system.exit이 자식 프로세스를 종료합니까?" –

+0

@NathanHughes 네, 제목에 덧붙여 더 의미가 있습니다. – RobinUS2

답변

5

당신은 확실히 종료하기 전에 완료하는 프로세스 기다려야합니다.

String line = null; 
final BufferedReader reader = 
    new InputStreamReader (process.getInputStream()); 
while((line = reader.readLine()) != null) { 
    // Ignore line, or do something with it 
} 

당신은 또한 마지막 부분에 대한 Apache IOUtils 같은 라이브러리를 사용할 수 있습니다 :이 시나리오 그러나 어떤 경우의 모범 사례에 적용 할 경우 확실하지.

+0

또한 프로세스의 inputstream 및 errorstream을 읽어야합니다. 그렇지 않으면 프로세스가 중단 될 수 있습니다. – GreyBeardedGeek

+0

여전히 작동하지 않습니다. 나는 현재 + process.getInputStream() 및 process.getOutputStream() 위의 모든 것을 가지고있다. 아무것도 실제로 작동하지 않는 것 같습니다. 다른 제안이 있으십니까? – RobinUS2

+0

정의가 작동하지 않습니까? 프로세스가 일찍 종료됩니까? 완료되지 않습니까? 당신은 프로세스의 출력 스트림을 소비하고 있습니까? – Perception

관련 문제