如何查看某 进程的CPU和MEM等信息呢?如果进程已经运行了很久了,如何才能确定真实的CPU利用率?那么 ps 和 top 命令可以妥妥的解决你的问题。
** 1. ps命令**
先来看ps
命令,通过ps -aux
可以查看到进程的CPU和MEM等信息:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.3 191808 3764 ? Ss 2018 2:44 /usr/lib/systemd/systemd --switched-root --system --deserialize 21root 2 0.0 0.0 0 0 ? S 2018 0:02 [kthreadd]root 3 0.0 0.0 0 0 ? S 2018 5:23 [ksoftirqd/0]root 5 0.0 0.0 0 0 ? S< 2018 0:00 [kworker/0:0H]root 7 0.0 0.0 0 0 ? S 2018 0:00 [migration/0]root 22056 0.1 2.0 750304 20340 ? Ssl 9月07 3:07 /usr/local/cloudmonitor/CmsGoAgent.linux-amd64......#后面内容省略
这里我们单独取PID为22056的CmsGoAgent进程的CPU和MEM信息。命令:ps -o %cpu %,mem -p 22056
[16210504@izuf60jasqavbxb9efockpz ~]$ ps -o %cpu,%mem -p 22056%CPU %MEM 0.1 2.0
由上面的结果可以看到成功拿出了结果,但是这个结果其实是不准确的,使用man ps
命令,查看ps
统计信息的说明如下:
%cpu %CPU cpu utilization of the process in "##.#" format. Currently, it is the CPU time used divided by the time the process has been running (cputime/realtime ratio), expressed as a percentage. It will not add up to 100% unless you are lucky. (alias pcpu).
%mem %MEM ratio of the process's resident set size to the physical memory on the machine, expressed as a percentage. (alias pmem).
我们可以看到ps命令统计CPU的信息是拿CPU使用的时间除以进行总运行的时间(cputime/realtime
ratio),这样就有可能出现,CPU在某个时间里利用率很高。但是因为进程已经运行了很久了,导致实际算出来的值依然是很小的,影响结果的判断。
** 2. top命令**
通常我们关注的是CPU在连续的变化情况,也就是需要CPU的切片信息。这个时候我们想要及时的统计信息就需要使用top
命令了:
[16210504@izuf60jasqavbxb9efockpz ~]$ toptop - 23:03:54 up 316 days, 13:52, 13 users, load average: 0.00, 0.01, 0.05Tasks: 159 total, 1 running, 113 sleeping, 45 stopped, 0 zombie%Cpu(s)