java에서 다른 프로세스의 독립 실행에 대하여

freechel의 이미지

제목을 맞게 쓴 것인지 모르겠습니다.

질문의 핵심은
실행되고 있는 프로세스에서 다른 프로세스를 원래 프로세스의 실행 여부와 관련없이 독립적으로 실행되도록 만들 수 있는가 하는 것입니다.

구체적으로 하고 싶은 일은
원래 프로세스(A.bat으로 실행)가 실행중에 자신을 업그레이드 할 파일이 있으면 다운로드 받은 후 자기 자신을 다시 실행하는 것입니다.

자기 자신을 다시 실행하는 것은 restart.bat이라는 파일에 A.bat을 호출하도록 만들고 A가 업그레이드 할 파일을 다운로드 받은 후 ProcessBuilder class를 사용하여 restart.bat을 호출합니다.

정리하면,
1. A 프로세스가 실행된다.
2. 업그레이드 할 파일을 다운로드한다.
3. restart.bat 파일을 호출한 후 1초 sleep한 후 A 프로세스는 종료(System.exit)된다.
4. 업그레이드 된 파일로 다시 A 프로세스가 실행된다.

이게 가능한 것인지요?
ProcessBuilder가 완전히 별도의 프로세스를 만드는 것이라면 가능할 것 같은데...
2~3과 관련된 코드는 아래와 같습니다.

    String[] cmd = { "cmd.exe", "/C", "restart.bat" };
    Process proc = new ProcessBuilder( cmd ).start();    // restart.bat 파일을 호출하여 A.bat를 실행하도록 한다.
// outputGobbler는 process 실행 시 출력을 read하는 class
    NetStreamGobbler outputGobbler = new NetStreamGobbler( proc.getInputStream(), "OUTPUT" );
    ret = outputGobbler.run();
    ret = proc.waitFor();
    proc.destroy();
    System.out.println("runtime worked?");
    Thread.sleep( 1000L );
    System.exit(1);              // 현재 실행중인 A 프로세스를 종료한다.

restart.bat 내용은 아래와 같습니다.

echo 'start sleeping..'
sleep 5
echo 'sleep 5'
./A.bat              // A 프로세스를 실행한다.

답변 꼭 부탁드립니다!

감사합니다.

cleol의 이미지

그냥 해보시면 될텐데 질문을 올리신 것을 보니 생각대로 안되신 모양이군요.
윈도는 아니고 리눅스에서 비슷한 일을 한 적이 있습니다.
최종적인 구현은 restarter 에게 현재 프로세스의 프로세스 번호를 알려주고,
restarter 가 현재 프로세스를 (ps 의 출력을 분석해서) 체크하다가 죽은 것이 확실하면
새로 프로세스를 생성하는 방식이었지만,
중간에 말씀하신 것과 거의 비슷한 방식으로 구현해서 테스트를 한 적이 있습니다.
문제없이 실행됐습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.