2

I want to run a shell script from within a Java program asynchronously-- i.e. after the java program starts execution of that shell script, it carries on with other operations-- and does some further work only when the shell script returns a response to it.. i.e. it does not explicitly stop and wait for the shell script's response.

Is this possible/feasible? How do I implement such functionality?

Basically i will be monitoring multiple servers using a single server that will manage all those servers-- for this it will run shell scripts on each of those servers...since there are many servers, and in java its recommended that number of threads not exceed number of cpu cores... hence I need a solution to this problem, that is not dependent on threading (because of threading limitations)...so that I can simultaneously (or near-simultaneously) fire off many such shell scripts without waiting for one of those scripts responses' (as waiting would affect processing for other shell script commands)... another issue.. the shell commands need to be invoked either on local machine or on remote machines and response is needed from both types of shell script execution(viz local execution and remote execution)...

Arvind
  • 6,404
  • 20
  • 94
  • 143

3 Answers3

5

did you try anything?

you can try something like:

ProcessBuilder builder = new ProcessBuilder("your command to launch script");
Process process = builder.start();

And it does NOT wait by default for the process to complete, so you can execute your code next.

And if you want to do some processing after the process is finished you can try:

int exitVal = process.waitFor();
vishal_aim
  • 7,636
  • 1
  • 20
  • 23
  • please go through my update to the question... kindly respond in case you have a solution that is workable for my use case... thanks – Arvind Jan 07 '13 at 07:52
  • what is the issue/problem in using the suggested approach, you can start many processes like this and do not need to wait, and if at the end you want to wait for processes to finish, just apply a loop and `waitFor` all the proccesses to finish (you can store process objects in a list) – vishal_aim Jan 07 '13 at 08:13
3

When you execute another process and want to obtain a result from it, you usually have to read the output of that process, as the process might block if its output buffer becomes full. The easiest way to achieve this is by having a single thread in your Java application which starts the script and then reads its output into some buffer. Other threads of the Java application can do whatever they want to do, and if the process is done, the thread can signal others about that event and then terminate.

I don't know where your recommendation to not use more threads than CPUs originates from, but I'd not hold with that in general. This is true for worker threads, where each active thread keeps one core busy, but in your case, most threads would be idle most of the time. There is some OS level resource overhead associated even with idle threads, so if there are really really many processes, using a single thread to read from all the streams would be better, but a lot more complicated.

MvG
  • 57,380
  • 22
  • 148
  • 276
  • please go through my update to the question... kindly respond in case you have a solution that is workable for my use case... thanks – Arvind Jan 07 '13 at 07:51
0

You can use Runtime.exec or ProcessBuilder in a different thread than your application main thread to run your shell script asynchronously.

This post shows how to use Runtime or ProcessBuilder. Read this post to learn java threads if you are not aware of it.

shazin
  • 21,379
  • 3
  • 54
  • 71