【问题标题】:Parse/Format ps -eo etime in minutes?在几分钟内解析/格式化 ps -eo etime?
【发布时间】:2012-12-17 17:42:12
【问题描述】:

有人可以帮我解析这个输出以显示总分钟数吗?

这是命令(除了格式之外,它按预期工作):

ps -eo pid,etime,command | grep some_process | grep -v grep | awk '{print $2}'

输出(以小时、分钟、秒为单位)

03:01:24

我需要输出类似于:

181.40

(3小时1分24秒显示为实数)

这可能吗?非常感谢任何建议。

【问题讨论】:

    标签: linux shell scripting


    【解决方案1】:
    ps -eo pid,etime,command | grep PID | grep -v grep | awk '{print $2}' | awk -F : '{ printf("%.2f\n", $1*60+$2+($3/60)); }'
    

    ;)

    已编辑: 改进版(谢谢@alexandernst 和@Nathan):

    ps -eo pid,etimes,command | grep PID | grep -v grep | awk '{printf("%.2f\n", $2/60)}'
    

    【讨论】:

    • 哇,太好了。这按预期工作。我能问一下背后的逻辑吗:$1*60+$2+($3/60) 谢谢你的快速回复
    • $1:$2:$3 = 小时:分钟:秒。小时到分钟 = $1 * 60。秒到分钟 = $3 / 60。明白了吗?
    • 如果/当 etime 不返回 HH:MM:SS,而只返回 MM:SS(例如,一个在不到一小时前开始的进程)时,这将失败
    • 看起来你可以使用 'etimes' 而不是 'etime' 直接获取秒数。
    • 'etimes' 关键字在 GNU ps 上可用,但在 BSD ps 上不可用,至少在 OSX 上不可用。更便携的解决方案出现在:Parse ps etime output into seconds
    猜你喜欢
    • 2013-01-17
    • 2020-07-21
    • 2015-10-06
    • 1970-01-01
    • 2018-12-04
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    相关资源
    最近更新 更多