Reputation: 385
I am creating a Process as follows:
Process p = Runtime.getRuntime().exec("nohup /usr/bin/python " + filePath + " &");
Process q = Runtime.getRuntime().exec("/usr/bin/python -m -webbrowser " + url);
I am trying to run the p
in the background so that q
can run without a problem. Also, when the Java program ends, the python script is no longer running.
Upvotes: 0
Views: 1085
Reputation: 48824
The command you ran is nohup
, and it did exit immediately. nohup
started a separate Python process, but that's not the process p
is controlling.
Why are you using nohup
and &
if that isn't the functionality you want? If you simply want to execute a Python subprocess just call:
Process p = Runtime.getRuntime().exec("/usr/bin/python " + filePath);
Note that it's also safer to use the overload of exec
that takes an array, so you don't have to do string-concatenation yourself (and therefore avoid a type of security exploit). The syntax changes slightly to:
Process p = Runtime.getRuntime().exec(new String[]{"/usr/bin/python", filePath});
Or with ProcessBuilder
:
Process p = new ProcessBuilder("/usr/bin/python", filePath).start();
Upvotes: 1
Reputation: 74
As Sean Bright mentioned take the '&' character out of the end of the script that you are running. Basically on all *nix systems. If you call make a type a command and use the ampersand, it will run that process in the background. That's why the call to java process is returning finished.
It would probably be better to not execute the command in the background (no '&') and just call Process.isAlive() periodically to determine if the process is finished.
Upvotes: 0
Reputation: 3228
Take a look at the documentation for the &
operator here. Your process is running in the background. As far as the Java program you're writing (and the shell, for that matter) are concerned, the call you made has already ended. It's most certainly still running in the background, but you're telling the shell (and by extension your Java program) to not pay attention to the output and move on.
Upvotes: 0