【问题标题】:How can I monitor the memory usage of a shell script at runtime?如何在运行时监控 shell 脚本的内存使用情况?
【发布时间】:2013-07-29 06:56:49
【问题描述】:

我喜欢这样的 shell 脚本:

while true;do
    curl -s example.com | egrep -o 'sth'
    sleep 60
done

如何在运行时获取进程 bashcurlegrepsleep 的总内存使用量?

【问题讨论】:

    标签: shell memory monitoring


    【解决方案1】:

    你可以使用 ps 命令:

     ps aux | grep <scriptName>
     ps aux | grep "curl -s example"
     ps aux | grep "egrap -s 'sth'"
     ps aux | grep "sleep 60"
    

    第 3 列和第 4 列是 cpu 和 mem(您可以使用 awk 获取它们)

    【讨论】:

    • 您还可以使用 grep 的 -e 选项将所有这四行合并为一行:ps axu | grep -e &lt;scriptname&gt; -e "curl -s example" -e "egrep -s 'sth'" -e "sleep 60" | cut -f3,4。即使您希望代码跨越多行(例如出于美观原因),您仍然可以通过以反斜杠 (``) 结束除最后一行之外的所有行来执行此操作,但是您只启动一个 grep 实例而不是四个实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2021-12-19
    • 2011-04-30
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多