gpt4 book ai didi

php - 获取 Linux 上 PHP 任务消耗的 CPU %

转载 作者:太空宇宙 更新时间:2023-11-04 05:58:21 25 4
gpt4 key购买 nike

如何使用 PHP 或 bash 获取 Linux 进程消耗的 CPU 百分比?我试图为此找到任何实用程序,但找不到。我发现总是因为同样的原因得到同样的结果。

最佳答案

我在这里找到了这个惊人的答案:https://unix.stackexchange.com/questions/554/how-to-monitor-cpu-memory-usage-of-a-single-process

要在脚本上使用该信息,您可以执行以下操作:

calcPercCpu.sh

#!/bin/bash
nPid=$1;
nTimes=10; # customize it
delay=0.1; # customize it
strCalc=`top -d $delay -b -n $nTimes -p $nPid \
|grep $nPid \
|sed -r -e "s;\s\s*; ;g" -e "s;^ *;;" \
|cut -d' ' -f9 \
|tr '\n' '+' \
|sed -r -e "s;(.*)[+]$;\1;" -e "s/.*/scale=2;(&)\/$nTimes/"`;
nPercCpu=`echo "$strCalc" |bc -l`
echo $nPercCpu

使用如下:calcPercCpu.sh 1234,其中 1234 是 pid

对于指定的$nPid,它将测量整1秒内10个CPU使用率快照的平均值(每个延迟0.1s * nTimes=10);这可以为当前发生的事情提供良好且快速的准确结果。

根据您的需要调整变量。

关于php - 获取 Linux 上 PHP 任务消耗的 CPU %,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40109251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com