Search code examples
androidshellhttp-redirectandroid-ndkadb

how to redirect the vmstat's output to a file in Android OS?


I connect to my rooted phone through adb shell command and I run the vmstat command so as to watch various system resources. Is there a way to redirect vmstat's output to a file. I tried:

vmstat > /sdcard/vmstat_output.txt

but it doesn't work.. It creates the file but there is no data inside..

Any ideas?


Solution

  • Ok, I fixed the problem. I download the source from here and I added one extra line:

    fflush(stdout);
    

    right after the print_line command (line: 134). Then, I cross-compiled the vmstat.c with agcc:

    agcc vmstat.c -o vmstat
    

    and put the file to the /sdcard/ through the adb:

    adb push vmstat /sdcard/
    

    Now the redirection works perfectly, as after every print_line call, the data are flushed to the file. As Mark Polhamus mentioned, the problem was the fact that vmstat is a block-buffered command, which means that a buffer has to be filled with data first and then this data will be flushed to the file..

    Thank you very much Mark! :)