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
favoretti@gmail.com - 04 Jul 2007 10:10 GMT
Oh yeah, and I forgot to add that running this script manually works
fine.
On 4 Jul, 11:07, favore...@gmail.com wrote:
> Hi,
>
[quoted text clipped - 12 lines]
> Regards,
> Vladimir
Gordon Beaton - 04 Jul 2007 10:41 GMT
> 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
[quoted text clipped - 3 lines]
>
> 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
--
favoretti@gmail.com - 04 Jul 2007 10:48 GMT
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
> > 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
[quoted text clipped - 13 lines]
>
> --
Gordon Beaton - 04 Jul 2007 10:59 GMT
> 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
--
getsanjay.sharma@gmail.com - 05 Jul 2007 17:51 GMT
> > 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. :)
[quoted text clipped - 16 lines]
>
> --
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
Gordon Beaton - 05 Jul 2007 18:18 GMT
> 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
--