Java Runtime.exec() and sudo.

Discussion in 'Java' started by favoretti@gmail.com, Jul 4, 2007.

  1. Guest

    Hi,

    I'm trying to run a unix script from the java applet that is running
    under tomcat.
    The problem I'm stuck with is the fact that certain part of the script
    has to be executed as a different user. I tried using both 'su' and
    'sudo', but the script seems to hang at the point of execution of
    those programs and blocks the whole thread, as I'm in need of the
    returncode of the script, so I'm using waitFor().

    Any ideas why it could hang?

    Thanks in advance.

    Regards,
    Vladimir
     
    , Jul 4, 2007
    #1
    1. Advertising

  2. Guest

    Oh yeah, and I forgot to add that running this script manually works
    fine.

    On 4 Jul, 11:07, wrote:
    > Hi,
    >
    > I'm trying to run a unix script from the java applet that is running
    > under tomcat.
    > The problem I'm stuck with is the fact that certain part of the script
    > has to be executed as a different user. I tried using both 'su' and
    > 'sudo', but the script seems to hang at the point of execution of
    > those programs and blocks the whole thread, as I'm in need of the
    > returncode of the script, so I'm using waitFor().
    >
    > Any ideas why it could hang?
    >
    > Thanks in advance.
    >
    > Regards,
    > Vladimir
     
    , Jul 4, 2007
    #2
    1. Advertising

  3. On Wed, 04 Jul 2007 02:07:28 -0700, wrote:
    > The problem I'm stuck with is the fact that certain part of the
    > script has to be executed as a different user. I tried using both
    > 'su' and 'sudo', but the script seems to hang at the point of
    > execution of those programs and blocks the whole thread, as I'm in
    > need of the returncode of the script, so I'm using waitFor().
    >
    > Any ideas why it could hang?


    One guess: su or sudo is prompting for a password.

    Another: the script produces output (stdout/stderr) that you aren't
    reading. Solution: read it _while_the_script_is_running_, or make the
    script run quietly.

    /gordon

    --
     
    Gordon Beaton, Jul 4, 2007
    #3
  4. Guest

    Gordon,

    Thanks for the ideas. The sudo does not prompt for a password, however
    the other idea about stdout/stderr seems valid, I indeed do not read
    the stdout/stderr. Is there any valid way in java to null-route that
    output? I don't really care what's happening there. I just need the
    return code. :) Adjusting the script at this moment is not an option,
    since it's not my creation, I just need to run it as-is.

    Vladimir

    On 4 Jul, 11:41, Gordon Beaton <> wrote:
    > On Wed, 04 Jul 2007 02:07:28 -0700, wrote:
    > > The problem I'm stuck with is the fact that certain part of the
    > > script has to be executed as a different user. I tried using both
    > > 'su' and 'sudo', but the script seems to hang at the point of
    > > execution of those programs and blocks the whole thread, as I'm in
    > > need of the returncode of the script, so I'm using waitFor().

    >
    > > Any ideas why it could hang?

    >
    > One guess: su or sudo is prompting for a password.
    >
    > Another: the script produces output (stdout/stderr) that you aren't
    > reading. Solution: read it _while_the_script_is_running_, or make the
    > script run quietly.
    >
    > /gordon
    >
    > --
     
    , Jul 4, 2007
    #4
  5. On Wed, 04 Jul 2007 02:48:17 -0700, wrote:
    > Is there any valid way in java to null-route that output? I don't
    > really care what's happening there. I just need the return code. :)


    Use redirection. Instead of this:

    String cmd = "myscript";
    Runtime.getRuntime().exec(cmd);

    do this:

    String[] cmd = {
    "/bin/sh",
    "-c",
    "myscript > /dev/null 2>&1"
    };
    Runtime.getRuntime().exec(cmd);

    /gordon

    --
     
    Gordon Beaton, Jul 4, 2007
    #5
  6. Guest

    On Jul 4, 2:59 pm, Gordon Beaton <> wrote:
    > On Wed, 04 Jul 2007 02:48:17 -0700, wrote:
    > > Is there any valid way in java to null-route that output? I don't
    > > really care what's happening there. I just need the return code. :)

    >
    > Use redirection. Instead of this:
    >
    > String cmd = "myscript";
    > Runtime.getRuntime().exec(cmd);
    >
    > do this:
    >
    > String[] cmd = {
    > "/bin/sh",
    > "-c",
    > "myscript > /dev/null 2>&1"
    > };
    > Runtime.getRuntime().exec(cmd);
    >
    > /gordon
    >
    > --


    Gordon, that seems to be a nice idea. But any reason why the array of
    Strings is needed instead of just running the entire thing as one
    single string? Which chain of reasoning was followed here to come to
    this conclusion?

    Thanks and regards,
    S T S
     
    , Jul 5, 2007
    #6
  7. On Thu, 05 Jul 2007 09:51:16 -0700, wrote:
    > Gordon, that seems to be a nice idea. But any reason why the array
    > of Strings is needed instead of just running the entire thing as one
    > single string? Which chain of reasoning was followed here to come to
    > this conclusion?


    What happened when you tried it with a single String?

    Since redirection is a shell feature, you need to specify a shell in
    the command passed to Runtime.exec(). It won't run your command in a
    shell otherwise.

    "sh -c" takes *one* argument.

    If you had used a single flat String it would have been passed to a
    StringTokenizer and broken into whitespace-separated elements,
    resulting in 4 arguments being passed to sh -c. The arrray prevents
    this.

    So by using a String array, you can make sure the command gets passed
    to the shell as a single argument in the same way as if you had
    enclosed the command within quotes on the (interactive) command line.

    /gordon

    --
     
    Gordon Beaton, Jul 5, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charlie Zhang
    Replies:
    5
    Views:
    6,568
    Qu├ębec
    Jun 19, 2004
  2. Jakir
    Replies:
    4
    Views:
    3,252
    Jakir
    Sep 1, 2004
  3. seenuvas
    Replies:
    7
    Views:
    2,683
    Roedy Green
    Jan 24, 2006
  4. seenuvas
    Replies:
    0
    Views:
    533
    seenuvas
    Jan 24, 2006
  5. Hal Vaughan
    Replies:
    11
    Views:
    1,195
    Gordon Beaton
    May 22, 2006
Loading...

Share This Page