如何查找占用 CPU 100% 的进程

如何查找占用 CPU 100% 的进程

当我查看 htop 和 top 时,我发现我的处理器使用率始终为 100%。但我看不到任何使用如此多 CPU 的进程。Htop 仅显示 1-2 个使用大约 5% CPU 时间的进程。

有没有办法找到占用那么多 CPU 时间的进程?

以下是输出ps -eo pcpu,pid,user,args | sort -r -k1 | less

%CPU   PID USER     COMMAND  
 0.8 20413 root     jsvc.exec -user tomcat -cp ./bootstrap.jar -Djava.endorsed.dirs=../common/endorsed -outfile ../logs/catalina.out -errfile ../logs/catalina.err -verbose org.apache.catalina.startup.Bootstrap -security  
 0.3   631 mysql    /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/mysql.pid --skip-external-locking  
 0.2  3380 root     /usr/local/apache/bin/httpd -k restart -DSSL  
 0.2 24698 root     tailwatchd  
 0.2 22472 root     /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/jakarta/tomcat/conf/logging.properties -Dfile.encoding=UTF8 -XX:MaxPermSize=128m -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/jakarta/tomcat/common/endorsed -classpath /usr/local/jakarta/tomcat/bin/bootstrap.jar -Dcatalina.base=/usr/local/jakarta/tomcat -Dcatalina.home=/usr/local/jakarta/tomcat -Djava.io.tmpdir=/usr/local/jakarta/tomcat/temp org.apache.catalina.startup.Bootstrap start  
 0.1 32095 root     cpanellogd - processing bandwidth  
 0.0  9733 root     sleep 1m  

答案1

pcpu没有达到你想象的效果。

UNIX 手册页:ps()

CODE    HEADER  DESCRIPTION

%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).

如您所见,它pcpu测量的不是当前 CPU 使用率,而是自进程启动以来的平均 CPU 使用率。如果某个进程在突然出现问题之前已经运行了几天,您将无法使用该ps命令查看该变化。

相反,在列top中显示当前的 CPU 使用率%CPU

UNIX 手册页:顶部 ()

k: %CPU  --  CPU usage
   The  task's  share  of  the  elapsed CPU time since the last screen
   update, expressed as a percentage of total CPU time. In a true  SMP
   environment, if  'Irix  mode'  is Off, top will operate in 'Solaris
   mode' where a task's cpu usage will be divided by the     total  number
   of  CPUs.   You toggle 'Irix/Solaris' modes with the 'I' interactive
   command.

top根据当前 CPU 使用率自动对进程进行排序,因此第一个进程应该是罪魁祸首。

相关内容