How to run from java-code command on Android?

cat /proc/kmsg > /mnt/sdcard/klog.log 

It means, how should the line Runtime.getRuntime.exec() look like?

    2 answers 2

    The fact is that over-melting cannot be done. You can execute the command with arguments. So you have to do something like this.

     Runtime.getRuntime().exec("/bin/sh -c 'cat /proc/kmsg > /mnt/sdcard/klog.log'"); 

    UPD

    Looks like I screwed up a bit too. Exec has some problem with executing commands with spaces in the arguments, so the code above will not work. I worked the following:

     ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "cat /proc/kmsg > ~/kmem"); pb.start().waitFor(); 
    • Ok. Tomorrow I will try, accomplish your goal. - kENNAAAAA
    • The file is created, but nothing is written there (((<code> new ProcessBuilder ("/ system / bin / sh", "- c", "cat / proc / kmsg> /mnt/sdcard/klog.log) .start () </ code> - kENNAAAAA
    • one
      waitFor? And there may be permission denied. If I were you, I would make a mergeErrorStream and read the output of the process and bring it somewhere to the screen. Surely you will see something interesting. - cy6erGn0m
    • In general, thanks for the help, close the topic - kENNAAAAA
    • Yes, I solved the problem by writing a native service with a socket, with root rights, it just happened. - kENNAAAAA

    Runtime.getRuntime().exec("cat /proc/kmsg > /mnt/sdcard/klog.log")

    But, most likely, it can not be done without root privileges.

    • root is. I tried this, it does not channel. Or is there another way to get a stream from the kernel logs? - kENNAAAAA
    • And what does not work? Any exception or something else? - Sergeich
    • There is no file. Checked through logcat -f /mnt/sdcard/logcat.log - there is a file. - kENNAAAAA
    • Matter of course it will not work. root has nothing to do with it. - cy6erGn0m