【发布时间】:2017-11-27 01:19:28
【问题描述】:
我正在尝试检索进程的 cpu 使用率 (%cpu) 并将其存储到变量中。 我可以使用 Shell 脚本显示 %cpu 和命令:
echo Enter the process name you wish to fetch:
read pn
#Displays %cpu of process
ps -eo %cpu,command | grep $pn
输出:
Enter the process name you wish to fetch:
terminal
0.0 grep terminal
'0.0' 定义 %cpu,'grep terminal' 定义命令(进程)。
如何将 cpu 使用率 (%cpu) 的值放入变量中:
declare -x cpuUsage=[%cpu of process]
echo $cpuUsage
【问题讨论】:
-
我不太确定
0.0 grep terminal正在输出你想要的东西。有关背景信息,请参阅此 question and its answers。