Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read superuser process output in Android

Tags:

android

I'm trying to run a shell command using superuser (su) periodically in the following way:

first I get the su process:

Process p = Runtime.getRuntime().exec("su");

then, periodically I run:

p.getOutputStream().write("some shell command".getBytes());
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = r.readLine()) != null) //Process output line

this problem is that the r.readLine() blocks and does not return never. only if I create a new su process and add p.getOutputStream().close() before reading output the code succeeds.

is there a way to use a single su process for issuing shell commands?

like image 247
Oren Avatar asked Aug 10 '26 11:08

Oren


1 Answers

FWIW, I've just come across the same issue, and it seems like I've resolved it by checking r.ready() on the BufferedReader. If there are no more lines to be read from the process output, then r.ready() returns false. I don't know how reliable this will be across different versions of the OS, but it works on 2.3.

Edit: also, before starting to read the output, make sure to sleep for a bit (say, 200ms) so that all of the output from your command gets in the buffer.

like image 114
Dmitry Brant Avatar answered Aug 14 '26 12:08

Dmitry Brant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!