1

i'm trying to execute a command with root privileges, i try this(only for example):

Process process = Runtime.getRuntime().exec(new String[]{"su","-c","whoami"});

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;

while ((line = reader.readLine()) != null) {
    result += line;
    Log.d(TAG,"RESULT:"+result);
}

But i need to call it in a cicle with hundred times, so this way is very slow and always is showing a dialog with the message of the root privileges are granted, so how i can create a sigle su process for write and read consecutively using input and output streams? i know to use both but i dont know how to write and the read the result of the command. Thanks for your help.

  • you expect 'whoami' to return something different after the first time? – GreyBeardedGeek Jan 04 '13 at 22:15
  • no, whoami is only for example, realy i want to execute a .sh file by command line, this file can do what ever i want and it return a string, the center of the problem is executing any command any number of times but with root privileges without call su by each one, my english is not good so i guess if i explained me well, and thanks. – Diego Fernando Murillo Valenci Jan 04 '13 at 22:47
  • I'd suggest writing another shell script that executes what you want in a loop, and calling that instead of the raw command. – GreyBeardedGeek Jan 04 '13 at 22:49
  • thanks, but i'm using a progress bar for track the progress, is not way to write and read in the Process through streams? – Diego Fernando Murillo Valenci Jan 04 '13 at 23:14
  • Yes, your sample code is already doing that, through the BufferedReader 'reader' – GreyBeardedGeek Jan 04 '13 at 23:17
  • yeah, in this case i am reading because i know that the command ends with one line, but when i use su is like to enter in another sesion in console, the while loop in that case never ends because su wait for any command and only ends if i enter the command exit, so i want to use su command then write a command through inputstream and inmediately read the output of that command using the outputstream but i dont know how to use the outputstream for that. – Diego Fernando Murillo Valenci Jan 04 '13 at 23:41
  • http://stackoverflow.com/questions/6896618/read-command-output-inside-su-process – Kozuch Feb 23 '17 at 12:40

0 Answers0