Nested processes

Hi,

Is there a way to use Runtime to run nested UNIX processes? For example, I would like to connect to a UNIX machine, then run a shell script on it. I have the following:

String connect ="ssh newUser@192.168.1.1";

String processToRun ="sh /home/newUser/test.sh";

Runtime rt = Runtime.getRuntime();

Process proc;

proc = rt.exec(connect);

proc = rt.exec(processToRun);

This, however, does not work. What happens is that it never terminates. What I believe happens is that once it executes the SSH command, the next SH command actually waits for SSH to terminate before it executes it. But then, the SSH process is, at the same time, waiting for a command. The thing is that I want the SH command to execute WITHIN the SSH process.

If I run this:

String connect ="ssh newUser@192.168.1.1";

String processToRun ="sh /home/newUser/test.sh";

Runtime rt = Runtime.getRuntime();

Process proc;

proc = rt.exec(connect +" " + processToRun);

This works ok because it is the same as simply running:

ssh newUser@192.168.1.1 sh /home/newUser/test.sh

right from UNIX which is ok. But this may be a problem if I want to run lots of processes within processes.

Can this be done?

Thanks!

[1422 byte] By [EmanuelVa] at [2007-11-26 19:52:13]
# 1

> String connect = "ssh newUser@192.168.1.1";

> String processToRun = "sh /home/newUser/test.sh";

>

> Runtime rt = Runtime.getRuntime();

> Process proc;

>

> proc = rt.exec(connect + " " + processToRun);

> This works ok because it is the same as simply

> running:

> ssh newUser@192.168.1.1 sh /home/newUser/test.sh

I am not familiar with the ssh command; however, it appears to take a command (string) as input. You can pass input to the process started with Runtime.exec() by using the Process.getOutputStream() method.

For more information read:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Fix formatting.

Message was edited by:

jbish

jbisha at 2007-7-9 22:42:57 > top of Java-index,Java Essentials,Java Programming...
# 2

> right from UNIX which is ok. But this may be a

> problem if I want to run lots of processes within

> processes.

Why is it a problem? That's part of SSH's functionality.If anything it's better because there would be one less process running on the remote host.

Anyway, if you really really want to log in, get a shell prompt, and then execute a script there, you can do that: just get the standard input, output, and error from the Process (which you should do anyway), and then write to the standard input stream the commands you want to run on the shell.

But just telling SSH to invoke the script you want to run remotely is going to be faster and easier and probably less resource-intensive than interacting with the shell.

paulcwa at 2007-7-9 22:42:57 > top of Java-index,Java Essentials,Java Programming...
# 3
Hi,Thanks for the responses. You're right. It would be slower to do it that way. I think I'll leave it the way I originally had it. But thanks for the response...I didn't think of writing to the standard input.
EmanuelVa at 2007-7-9 22:42:57 > top of Java-index,Java Essentials,Java Programming...
# 4
That's my point -- the way you had it to begin with was better.
paulcwa at 2007-7-9 22:42:57 > top of Java-index,Java Essentials,Java Programming...