V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2018-05-24 14:44:48 +08:00 · 3221 次点击
    这是一个创建于 2161 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我想达到的目的是,输出实时保存到 txt 里(同时每行加上时间)且在 cmd 里也实时打印出来。
    10 条回复    2018-05-29 19:30:34 +08:00
    fcce
        1
    fcce  
       2018-05-24 14:54:06 +08:00
    shell 直接就搞定了,为什么要用 python,|tee 了解一下?
    zjb861107
        2
    zjb861107  
       2018-05-24 14:54:57 +08:00
    重定向到文件,Python 读取文件
    frmongo
        3
    frmongo  
    OP
       2018-05-24 15:16:15 +08:00
    @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  
       2018-05-24 16:45:25 +08:00
    @frmongo 另开一个窗口, tailf aa.txt
    ptrees
        5
    ptrees  
       2018-05-24 16:55:57 +08:00
    额..没看仔细,怎么删掉自己的回复...
    jalena
        6
    jalena  
       2018-05-24 17:03:36 +08:00
    adb shell top -m 10 |grep com.jingdong.app.mall >> tee aa.txt
    jalena
        7
    jalena  
       2018-05-24 17:03:53 +08:00
    adb shell top -m 10 |grep com.jingdong.app.mall >> aa.txt
    Loner233
        8
    Loner233  
       2018-05-24 17:25:18 +08:00
    >>> from subprocess import check_output
    >>> out = check_output(["ls", "-al"])
    frmongo
        9
    frmongo  
    OP
       2018-05-24 20:33:06 +08:00
    最后我用 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  
       2018-05-29 19:30:34 +08:00
    刚想问下楼主怎么实现的,因为我最近也想直接想办法比对 logcat 里面的关键参数;但是自己看了下你这个监控 top,每次运行至请求一次,好像 logcat 也用不了....
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5405 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 08:36 · PVG 16:36 · LAX 01:36 · JFK 04:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.