0

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.

SVN600
  • 345
  • 1
  • 5
  • 19

3 Answers3

1

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();
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • thanks for the help. The problem I seem to be having is that the background process is actually ending immediately. When I run the same command from the terminal it works fine, but when I run it from my Java program and then look at all the python processes running it is not listed. When I go to use the web app it should be launching it is unable to connect – SVN600 Jul 06 '16 at 13:29
  • @SVN600 try adding more logging and debugging code. Specifically [wait](https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html#waitFor--) for the process to finish then print out its [exit code](https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html#exitValue--), [stdout](https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html#getInputStream--), and [stderr](https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html#getErrorStream--). These should provide some hints as to why your process is crashing. – dimo414 Jul 06 '16 at 17:07
0

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.

Community
  • 1
  • 1
Athena
  • 3,200
  • 3
  • 27
  • 35
0

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.