Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在用 Java 开发一个小型实用程序,用于监控远程 Linux 机器的 CPU。我正在使用 usingsar命令来监视它。问题是,当我写sar 1 1它需要一些时间来获得 cpu 使用率。我试过sar 0了,但它显示相同的 cpu 值而不是在线 cpu 使用情况。我该怎么做才能立即使用 cpu?
sar
sar 1 1
sar 0
一种选择是:
top
或者
top | grep <App_name>
ps -p <app_pid> -o %cpu
编辑
仅获取某个 pid 的 cpu 使用百分比:
ps -p PID -o %cpu | sed 's/[^0-9]//g' | tr -d '\n'
ps -p PID -o %cpu | sed -n 2p | sed 's/ //'