Showing results for 
Search instead for 
Did you mean: 

running script in background thru Runtime.exec() hangs

Honored Contributor

Re: running script in background thru Runtime.exec() hangs


Ok, I try with a modified SubTest which is in an infinite loop:

class SubTest
SubTest (String [] args)
for (;;) {
System.out.println ("arg = " + args[0]);
Thread.currentThread().sleep(Long.parseLong (args[0]) * 1000);
} catch (Exception e)
e.printStackTrace ();
public static void main (String [] args)
new SubTest (args);

The results are identical to the previous test. The process remains active until my kill command. I did all 3 tests (testa,testb and SubTest without any shell script)

Honored Contributor

Re: running script in background thru Runtime.exec() hangs

I did some new tests. Different environment:
Linux - RedHat 9
Java -
java version "1.3.1"
jdkgcj 0.2.3 (
gcj (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)


java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)

I add two lines to the testb script before running the java SubTest:

read x
echo $x >> /home/bojan/tmp/test.log

In I read one line from the process output stream and write one line to the input stream with:

OutputStreamWriter os = new OutputStreamWriter (p.getOutputStream ());
os.write ("test\n" , 0 , 5);
os.flush ();

and exit from program.
This works ok. Then I delete the line which does flush. With gcj all ok, but with Sun java the process die.

New Member

Re: running script in background thru Runtime.exec() hangs

hi Bojan,

It seems that if I do not access the streams, the process continues to work. The problem with regard to the hanging is solved. But the pitfall here is that I cannot get the output of the program.

The hanging issue comes into picture when I try to access the InputStream of the process. For now, I have created a workaround. I call my main script from another script, which has mainscript >> mainscript.log, and then from the java file I empty the contents of mainscript.log to the console. So job done! Your point about the OutputStream was really helpful. Thanks a bunch!

Now, I want to see the reason why the programs hangs when we try to access the InputStream? Any ideas?
Honored Contributor

Re: running script in background thru Runtime.exec() hangs


Try to ask Google "java process stream hang"

There are many descriptions of this simptom. Maybe some of them will explain yours questions.

Honored Contributor

Re: running script in background thru Runtime.exec() hangs

Shiva, Mohammed,

My considerations abbout standard I/O streams (not tested!).
I think that, if you have a background process, which will live alone after the main process will exit, you must reassign all three streams to something known (probably files) with System.setErr(), System.setIn() and System.setOut(). If you do not do that, the streams are cuted out when the main process exits and writing or reading will produce some strange effects (hangs, exceptions etc). For the communication between the main process and the background process you must use some other method (maybe sockets) or implement some sort of "protocol" which will tell to both sides, when the communication is finished. When the communication is finished the background process will reassign the streams and the main process will not try to write or read to the streams. Probably you can close them (but this must be tested).

Another time: This are my considerations which was not tested. When you get in such troubles, it is a good practice to write some small programs to test the behavior (and with Java that should be done on different OSes).

Occasional Contributor

Re: running script in background thru Runtime.exec() hangs

Hi Chaps

I have followed your thread with interest as I am having difficulties in similar areas.

- I am writing an application that mimicks a high availability solution so that I can run a configurable number of processes on a configurable number of linux servers. On each box resides a java process (Daemon) that starts the processes for me based on input from a remote server (uses RMI).

- unable to stop an external process

- if I run a c++ executable directly (for example a simple compiled exe that just does some looping) and I call destroy on the process, the exe dies as I would expect
- if I create a very simple shell script and run it and then try and kil it all is well .. eg
#!/bin/csh -f
while (1)
echo "hello"
- if I then try to run a java process in a ksh script and try and kill it, the destroy method does not actually kill the process at all .. a really nasty one if you are trying to abstract the OS/host from the user. Have you come accross this? Have you tried to run other java processes with the java.lang.Process class?