V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
frmongo
V2EX  ›  Python

我有一个命令 adb shell top -m 10 |grep myappname 输入后命令行可以周期地打印 cpu 占用率。如何用 Python 抓取输出?

  •  
  •   frmongo · May 24, 2018 · 3930 views
    This topic created in 2899 days ago, the information mentioned may be changed or developed.
    我想达到的目的是,输出实时保存到 txt 里(同时每行加上时间)且在 cmd 里也实时打印出来。
    10 replies    2018-05-29 19:30:34 +08:00
    fcce
        1
    fcce  
       May 24, 2018
    shell 直接就搞定了,为什么要用 python,|tee 了解一下?
    edsion996
        2
    edsion996  
       May 24, 2018
    重定向到文件,Python 读取文件
    frmongo
        3
    frmongo  
    OP
       May 24, 2018
    @kalelfc 谢谢,我又有个问题了,adb shell top -m 10 |tee aa.txt 是 work 的
    adb shell top -m 10 |grep com.jingdong.app.mall|tee aa.txt 就没有输出了
    ptrees
        4
    ptrees  
       May 24, 2018
    @frmongo 另开一个窗口, tailf aa.txt
    ptrees
        5
    ptrees  
       May 24, 2018
    额..没看仔细,怎么删掉自己的回复...
    jalena
        6
    jalena  
       May 24, 2018
    adb shell top -m 10 |grep com.jingdong.app.mall >> tee aa.txt
    jalena
        7
    jalena  
       May 24, 2018
    adb shell top -m 10 |grep com.jingdong.app.mall >> aa.txt
    Loner233
        8
    Loner233  
       May 24, 2018
    >>> from subprocess import check_output
    >>> out = check_output(["ls", "-al"])
    frmongo
        9
    frmongo  
    OP
       May 24, 2018
    最后我用 python 写了一个,shell 也可以做到 while true;do (adb shell top -m 10 -n 1 |grep com.jingdong.app.mall|tee -a aa.txt);done
    HLLiu
        10
    HLLiu  
       May 29, 2018
    刚想问下楼主怎么实现的,因为我最近也想直接想办法比对 logcat 里面的关键参数;但是自己看了下你这个监控 top,每次运行至请求一次,好像 logcat 也用不了....
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2529 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 06:26 · PVG 14:26 · LAX 23:26 · JFK 02:26
    ♥ Do have faith in what you're doing.