How to run a shell script asynchronously from within a Java program

I want to run a shell script from an asynchronous Java program, that is, after the java program starts executing that shell script, it will continue with other operations and only continue running if the shell script returns a response to it, that is, it does not explicitly stop and does not wait for shell script response.

Is it possible / possible? How do you implement such functions?

Basically I will control multiple servers with one server that will manage all of these servers - for that it will run shell scripts on each of these servers ... as there are many servers and in java it is recommended that the number of threads does not exceed the number of cpu cores ... so I need a solution to this problem that does not rely on streaming (due to streaming restrictions) ... so that I can simultaneously (or almost simultaneously) knock many of these shell scripts down without waiting for a response to one of these script responses "(since waiting will affect processing for other shell script commands) ... another problem .. shell commands need to be called either on the local machine or on remote machines and the response is needed to execute both types of shell script (for examplelocal execution and remote execution) ...

+2


source to share


3 answers


Have you tried anything?

you can try something like:

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

      



And you MUST NOT wait by default for the process to complete, so you can execute the following code.

And if you want to do some post-process processing, you can try:

int exitVal = process.waitFor();

      

+3


source


When you are executing another process and want to get a result from it, you usually need to read the result of that process, as the process can block if its output buffer fills up. The easiest way to achieve this is to create a single thread in your Java application that runs the script and then reads its output into some buffer. Other threads in a Java application can do whatever they want, and if the process is running, the thread can signal this event to others and then terminate.



I don't know where your recommendation to not use more threads than CPUs comes from, but I would not stick with it in general. This is true for worker threads, where each active thread supports one main occupation, but in your case, most threads will be idle most of the time. There are some OS level resources associated with even simple streams, so if there are indeed many processes, using a single stream to read from all streams would be better, but much more difficult.

+2


source


You can use Runtime.exec or ProcessBuilder on a different thread than the main thread of the application to run the shell script asynchronously.

This post shows how to use Runtime or ProcessBuilder. Read this post to learn Java streams if you don't know about it.

0


source







All Articles