Failed to run grep command

I tried to run the following command,

Process p = Runtime.getRuntime().exec("/system/bin/lsof|grep mediaserver"); 

In android (java), but I get an error. if I run the following command

  Process p = Runtime.getRuntime().exec("/system/bin/lsof "); 

file saved successfully. Can someone say what a mistake is? Actually I want to list and check if the media server service is working or not.

0
source share
2 answers

The grep utility cannot be installed on your device.

You can verify this by trying the following on the console:

 > adb shell $ grep grep: not found 

The last line indicates that this command is not available.

+1
source

The problem is that Runtime.getRuntime().exec(...) does not know how to handle the shell language. On a Linux / Unix platform, you will have something like this:

 Process p = Runtime.getRuntime().exec(new String[]{ "/bin/sh", "-c", "/system/bin/lsof | grep mediaserver"}); 

However (apparently) Android does not have a shell / command line by default. Therefore, either you need to identify and install a suitable shell on your device, or build the pipeline "manually"; that is, by creating a pipe file descriptor and executing two commands so that lsof writes to the pipe and grep lsof from it.

Maybe the answer is to run it like this ...

 Process p = Runtime.getRuntime().exec( "adb shell /system/bin/lsof | grep mediaserver"); 

(Try running the "shell ..." part of adb interactively before doing this with Java.)

0
source

Source: https://habr.com/ru/post/1209753/


All Articles