在上一篇文章中我们讨论了如何在Linux中找到最耗内存的进程。在Linux系统中你也可能会遇到发现CPU使用率高的进程的情况。如果是这样,那么您需要列出系统上的高CPU密集型进程以确保确定。我认为只有两种方法可以做到:使用top命令和ps命令。出于某些原因,我更喜欢使用top命令而不是ps命令。但是这两种工具都可以实现你想要的,所以你可以根据自己的需要来决定使用哪一种。这两种工具都被Linux系统管理员广泛使用。1)如何使用top命令找出Linux中CPU占用率高的进程在众多Linux系统性能监控工具中,Linuxtop命令是最好用也是最知名的一个。top命令提供了Linux系统运行进程的动态实时视图。它显示系统概览信息和当前由Linux内核管理的进程列表。它显示了很多系统信息,如CPU使用率、内存使用率、交换内存、正在运行的进程数、当前系统正常运行时间、系统负载、缓冲区大小、缓存大小、进程PID等等。默认情况下,top命令的输出按CPU使用率排序,结果每5秒更新一次。如果您想更清晰地查看更深入的分析结果,以批处理模式运行top命令是最好的方法。同时需要理解top命令输出的含义,从而解决系统的性能问题。#顶部-b|head-50top-00:19:17up14:23,1user,loadaverage:2.46,2.18,1.97Tasks:306total,1running,305sleeping,0stopped,0zombie%Cpu0:10.4us,3.0sy,0.0ni,83.9id,0.0wa,1.3hi,1.3si,0.0st%Cpu1:17.0us,3.0sy,0.0ni,78.7id,0.0wa,0.3hi,1.0si,0.0st%Cpu2:13.0us,4.0sy,0.0ni,81.3id,0.0wa,0.3hi,1.3si,0.0st%Cpu3:12.3us,3.3sy,0.0ni,82.5id,0.3wa,0.7hi,1.0si,0.0st%Cpu4:12.2us,3.0sy,0.0ni,82.8id,0.7wa,0.3hi,1.0si,0.0st%Cpu5:6.4us,2.7sy,0.0ni,89.2id,0.0wa,0.7hi,1.0si,0.0st%Cpu6:26.7us,3.4sy,0.0ni,68.6id,0.0wa,0.7hi,0.7si,0.0st%Cpu7:15.6us,4.0sy,0.0ni,78.8id,0.0wa,0.7hi,1.0si,0.0stKiBMem:总计16248556,免费1448920,使用8571484,6228152buff/cacheKiB交换:总计17873388,免费17873388,使用0。4596044可用MemPID用户PRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND2179daygeek203106324613584327564S79.53.814:19.76WebContent1714daygeek204603372974600403504S20.26.065:18.91firefox1227daygeek204192012376332180348S13.92.320:43.26gnome-shell18324DayGeek203296192766040127948S6.34.79:18.12Web内容1170dayGeek201008264572036572036572036546180S6.03.53.518:07.85gS3.011.014:09.65VirtualBoxVM1211daygeek9-1128652682103216588S2.00.110:46.37pulseaudio4562daygeek2010968882881221044S1.70.24:42.93VBoxSVC1783daygeek203123888376896134788S1.32.339:32.56WebContent3286daygeek203089736404088184968S1.02.541:57.44网页内容上面命令各部分解释:top:command-b:batchfilemodehead-50:显示输出的前50个PID:进程的IDUSER:进程的所有者PR:进程的级别NI:进程的NICE值VIRT:进程使用的虚拟内存RES:进程使用的物理内存SHR:进程使用的共享内存S:这个值表示进程的状态:S=睡眠,R=运行,Z=僵尸进程%CPU:进程使用的CPU百分比%MEM:进程使用的RAM百分比TIME+:进程已经运行了多长时间COMMAND:进程名称如果你想查看命令的完整路径而不是命令名称,请按以下格式运行top命令:#top-c-b|head-50top-00:28:49up14:33,1user,loadaverage:2.43,2.49,2.23Tasks:305total,1running,304sleeping,0stopped,0zombie%Cpu0:11.7us,3.7sy,0.0ni,82.3id,0.0wa,1.0hi,1.3si,0.0st%Cpu1:13.6us,3.3sy,0.0ni,81.1id,0.7wa,0.3hi,1.0si,0.0st%Cpu2:10.9us,2.6sy,0.0ni,85.1id,0.0wa,0.3hi,1.0si,0.0st%Cpu3:16.0us,2.6sy,0.0ni,80.1id,0.0wa,0.3hi,1.0si,0.0st%Cpu4:9.2我们,3.6sy,0.0ni,85.9id,0.0wa,0.3hi,1.0si,0.0st%Cpu5:15.6us,2.9sy,0.0ni,80.5id,0.0wa,0.3hi,0.7si,0.0st%Cpu6:11.6us,4.3sy,0.0ni,82.7id,0.0wa,0.3hi,1.0si,0.0st%Cpu7:8.0us,3.0sy,0.0ni,87.3id,0.0wa,0.7hi,1.0si,0.0stKiBMem:16248556total,1022456free,8778508used,2buffB缓存.9Ki644/7:总计17873388个,免费17873388个,使用0个。4201560availMemPIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND18527daygeek203151820624808325748S52.83.859:26.72/usr/lib/firefox/firefox-contentproc-childID18-isForBrowser-prefsMap043parent03Len-1020191031132559-greomni/+1714daygeek204764668910940443228S21.55.668:59.33/usr/lib/firefox/firefox--new-window1227daygeek204193108377344181404S11.62.321:47.36/usr/bin/gnome-shell1170daygeek201008820572700546844S5.63.519:05.10/usr/lib/Xorgvt2-displayfd3-auth/run/user/1000/gdm/Xauthority-nolistentcp-backgroundnone-n??oreset-keeptty-verbose3183022889daygeek344127948S5.04.99:46.89/usr/lib/firefox/firefox-contentproc-childID16-isForBrowser-prefsLen10002-prefMapSize213431-parentBuildID20191031132559-greomni/+4684daygeek2033lib/2virtual1.box/Box13708虚拟--commentCentOS7--startvm002f47b8-2af2-48f5-be1d-67b67e03514c--no-startvm-errormsgbox4607daygeek2045910401.7g1.6gS3.011.014:28.86/usr/lib/virtualbox/virtualbox/VirtualBoxVM--评论Ubuntu-18.04--startvme8c32dbb-8b01-41b0-977a-bf28b9db1117--no-startvm-errormsgbox1783daygeek203132640451924132168S2.62.839:49.66/usr/lib/firefox/firefox1-content-childIDBrowserprefslen1-prefmapsize213431-parentbure201910313556-greomni/USR121289044S1.70.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.20.24:49.33/usr/lib/virtualbox/VBoxSVC--auto-shutdown16865daygeek203073364430596124652S1.32.78:04.02/usr/lib/firefox/firefox-contentproc-childID15-isForBrowser-prefsLen10002-prefMapSize213431-parentBuildID20191031132559-greomni/+2179daygeek202945348429644172940S1.02.615:20.90/usr/lib/firefox/firefox-contentproc-childID6-isForBrowser-prefsLen7821-prefMapSize213431-parentBuildID20191031132559-greomni/us+2)如何在Linux中处理高CPU使用率ps是processstatus进程状态的缩写,可以显示系统中活跃/运行进程的信息。它提供当前进程及其详细信息,例如用户名、用户ID、CPU使用率、内存使用率、进程启动日期时间快照、命令名称等。#ps-eopid,ppid,%mem,%cpu,cmd--sort=-%cpu|头部PIDPPID%MEM%CPUCMD1852717144.240.3/usr/lib/firefox/firefox-contentproc-childID18-isForBrowser-prefsLen10002-prefMapSize213431-parentBuildID20191031132559-greomni/usr/lib/firefox/omni.ja-appomni.jausr/lib/firefox/browser/omni.ja-appdir/usr/lib/firefox/browser1714truetab171411525.68.0/usr/lib/firefox/firefox--new-window1832417144.96.3/usr/lib/firefox/firefox-contentproc-childID16-isForBrowser-prefsLen10002-prefMapSize213431-parentBuildID20191031132559-greomni/usr/lib/firefox/omni.ja-appomni/usr/lib/firefox/browser/omni.ja-appdir/usr/lib/firefox/browser1714true选项卡328617142.05.1/usr/lib/firefox/firefox-contentproc-childID14-isForBrowser-prefsLen8078-prefMapSize213431-parentBuildID20191031132559-greomni/usr/lib/firefox/omni.ja-appomni.jausr/lib/firefox/browser/omni.ja-appdir/usr/lib/firefox/browser1714truetab178317143.04.5/usr/lib/firefox/firefox-contentproc-childID1-isForBrowser-prefsLen1-prefMapSize213431-parentBuildID20191031132559-greomni/usr/lib/firefox/omni.ja-appomni/usr/lib/firefox/浏览器/omni.ja-appdir/usr/lib/firefox/browser1714truetab122711522.32.5/usr/bin/gnome-shell117011683.52.2/usr/lib/Xorgvt2-displayfd3-auth/run/user/1000/gdm/Xauthority-nolistentcp-backgroundnone-n??oreset-keeptty-详细31686517142.52.1/usr/lib/firefox/firefox-contentproc-childID15-isForBrowser-prefsLen10002-prefMapSize213431-parentBuildID20191035us139-lib/firefox/omni.ja-appomni/usr/lib/firefox/browser/omni.ja-appdir/usr/lib/firefox/browser1714truetab217917142.71.8/usr/lib/firefox/firefox-contentproc-childID6-isForBrowser-prefsLen7821-prefMapSize213431-parentBuildID20191031132559-greomni/usr/lib/firefox/omni.ja-appomni/usr/lib/firefox/browser/omni.ja-appdir/usr/lib/firefox/browser1714truetab上面命令各部分解释:ps:命令名-e:选择所有进程-o:自定义输出格式–sort=-%cpu:根据CPU使用率排序输出theresultshead:显示结果的前10行PID:进程IDPPID:父进程ID%MEM:进程使用的RAM百分比%CPU:进程占用的CPU百分比Command:进程名想要查看命令名称而不是运行ps命令的命令的绝对路径,格式如下:#ps-eopid,ppid,%mem,%cpu,comm--sort=-%cpu|headPIDPPID%MEM%CPUCOMMAND1852717144.140.4WebContent171411525.78.0firefox1832417144.96.3WebContent328617142.05.1WebContent178317143.04.5WebContent122711522.32.5gnome-shell117011683.52.2Xorg1686517142.42.1网页内容217917142.71.8网页内容
