Java Runtime.exec() and sudo.

F

favoretti

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
 
G

Gordon Beaton

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

--
 
F

favoretti

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
 
G

Gordon Beaton

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

--
 
G

getsanjay.sharma

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
 
G

Gordon Beaton

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

--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top