What happens to STDOUT from ANDROID Runtime.GetRuntime().exec?
<p>I have some code running under ANDROID 4.1 on a rooted tablet:-</p>

<pre><code>String[] command = { "/system/xbin/su", "-c", "/data/mystuff/bin/init.sh" }; 
Process proc = Runtime.getRuntime().exec(command); 
proc.waitFor(); 
</code></pre>

<p>The idea is to run a Linux initialisation script.  The script looks a bit like this...</p>

<pre><code>#!/system/bin/sh
/data/mystuff/bin/script1 &amp;
/data/mystuff/bin/script2 &amp;
/data/mystuff/bin/script3 &amp;
</code></pre>

<p>The scripts are all bash scripts which loop forever doing interesting stuff.  However, when I run "ps" from an adb shell I can only see script1 and script3 - script2 has mysteriously disappeared.</p>

<p>As far as I can tell, script2 disappears (killed?) if it writes to stdout a few times (it has a few "echo" statements in it to help with debugging).  The reason I say this is because if I change the init.sh script to do</p>

<pre><code>/data/mystuff/bin/script2 &gt;/dev/null 2&gt;&amp;1 &amp;
</code></pre>

<p>then script2 works flawlessly.  This has to be something to do with where the stdout output is going.  script2 is inheriting its stdout stream from init.sh, and init.sh is getting it from <code>Runtime.getRuntime().exec(command)</code>, so what is it getting, and why does a process get killed when it writes to it a few times?</p>

<p>The obvious fix is to put</p>

<pre><code>/exec &lt;/dev/null &gt;/dev/null 2&gt;&amp;1
</code></pre>

<p>at the top of my init.sh script to detach it and its children from the parent stdin, stdout and stderr streams, but I'd be interested to know why it's breaking in the first place.</p>