开发者

How I can simulate "tail" command for file on the Android file system?

开发者 https://www.devze.com 2023-01-02 12:45 出处:网络
I have file on SD-CARD and my app using it as log file. Is it possible through the adb to watch file with all changes in re开发者_如何学JAVAal time?

I have file on SD-CARD and my app using it as log file. Is it possible through the adb to watch file with all changes in re开发者_如何学JAVAal time? Like with tail -f /sdcard/myfile.log command.


This seems to work great for me:

adb shell "while true; do cat; sleep 1; done < /sdcard/myfile.log"


You can install busybox and then:

adb shell
tail -f /path/of/your/file

But remember that you should have root access to install busybox. If you are using the emulator check this one: How to get root access on Android emulator?


You can do this with logcat. You can add a view that will only show log entries from your app and it will be continuously updated.


There is a great app for this: Terminal IDE. It contains many linux commands, and it does not need root access. You can install it from GooglePlay. Is is free of charge (and open source, GPLv2).

One of its best features is that it can be used through telnet. Start it on your phone, and type telnetd command. It will start a telnet daemon, which listens on port 8080 by default. After that you can connect it from your PC, with the following command: (use cygwin on windows)

telnet 192.168.1.8 8080

You should use your phone's IP address instead of the above one. After a successful connection you will have an arbitrary sized terminal on your PC, which is capable to run tail -f command on your phone. And many others, such as bash and all of its builtin commands.


Building upon Jesse's answer, to do similar with a file within an app's private storage area:

adb shell "while true; do run-as com.yourdomain.yourapp cat /data/data/com.yourdomain.yourapp/app_flutter/yourfile.txt; sleep 5; done" | egrep -o 'sometext.{0,50}'

(This example is for a flutter app on Android, but is similar minus the app_flutter directory.)

do run-as changes the user under which the command is run to the application. By default adb shell user shouldn't have access to any files under an application's private storage area.

| egrep -o 'sometext.{0,50}' the cat command sends the file contents to STDOUT. egrep is taking the contents & searching for -o (only) sometext + 50 characters" using regex (hence egrep instead of grep).

Last Line Only

Replace cat with tail -n 1.

Add --line-buffered to egrep

adb shell "while true; do run-as com.yourdomain.yourapp tail -n 1 /data/data/com.yourdomain.yourapp/app_flutter/yourfile.txt; sleep 5; done" | egrep --line-buffered -o 'sometext.{0,50}'

0

精彩评论

暂无评论...
验证码 换一张
取 消