Linux下如何查看哪些查看某个进程占用内存的CPU内存资源最多

linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合:ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|headlinux下获取占用内存资源最多的10个进程,可以使用如下命令组合:ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head&&&&命令组合解析(针对CPU的,MEN也同样道理):ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head该命令组合实际上是下面两句命令:ps aux|head -1ps aux|grep -v PID|sort -rn -k +3|head
可以使用一下命令查使用内存最多的10个进程
查看占用cpu最高的进程
aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head
或者top (然后按下M,注意这里是大写)
查看占用内存最高的进程
ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head
或者top (然后按下P,注意这里是大写)
该命令组合实际上是下面两句命令:
ps aux|head -1ps aux|grep -v PID|sort -rn -k +3|head
其中第一句主要是为了获取标题(USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND)。接下来的grep -v PID是将ps aux命令得到的标题去掉,即grep不包含PID这三个字母组合的行,再将其中结果使用sort排序。sort -rn -k +3该命令中的-rn的r表示是结果倒序排列,n为以数值大小排序,而-k +3则是针对第3列的内容进行排序,再使用head命令获取默认前10行数据。(其中的|表示管道操作)
补充:内容解释
PID:进程的IDUSER:进程所有者PR:进程的优先级别,越小越优先被执行NInice:值VIRT:进程占用的虚拟内存RES:进程占用的物理内存SHR:进程使用的共享内存S:进程的状态。S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值为负数%CPU:进程占用CPU的使用率%MEM:进程使用的物理内存和总内存的百分比TIME+:该进程启动后占用的总的CPU时间,即占用CPU使用时间的累加值。COMMAND:进程启动命令名称
、可以使用以下命令查使用内存最多的K个进程
ps -aux | sort -k4nr | head -K
如果是10个进程,K=10,如果是最高的三个,K=3
说明:ps -aux中(a指代all&&所有的进程,u指代userid&&执行该进程的用户id,x指代显示所有程序,不以终端机来区分)
&&&&&&& ps -aux的输出格式如下:
PID %CPU %MEM
STAT START
TIME COMMAND
0:00 /sbin/init
0:00 [kthreadd]
0:11 [migration/0]
&&&&&sort -k4nr中(k代表从第几个位置开始,后面的数字4即是其开始位置,结束位置如果没有,则默认到最后;n指代numberic sort,根据其数值排序;r指代reverse,这里是指反向比较结果,输出时默认从小到大,反向后从大到小。)。本例中,可以看到%MEM在第4个位置,根据%MEM的数值进行由大到小的排序。
&&&&&head -K(K指代行数,即输出前几位的结果)
&&&&&|为管道符号,将查询出的结果导到下面的命令中进行下一步的操作。
方法2:top&(然后按下M,注意大写)
二、可以使用下面命令查使用CPU最多的K个进程
ps -aux | sort -k3nr | head -K
方法2:top&(然后按下P,注意大写
阅读(...) 评论()详解Linux如何查看当前占用CPU或内存最多的几个进程
字体:[ ] 类型:转载 时间:
本篇文章主要介绍了详解Linux如何查看当前占用CPU或内存最多的几个进程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
ps -aux | sort -k4nr | head -N
命令详解:
1、head:-N可以指定显示的行数,默认显示10行。
2、ps:参数a指代all——所有的进程,u指代userid——执行该进程的用户id,x指代显示所有程序,不以终端机来区分。
ps -aux的输出格式如下:
PID %CPU %MEM
STAT START
TIME COMMAND
0:00 /sbin/init
0:00 [kthreadd]
0:11 [migration/0]
3、sort -k4nr中(k代表从根据哪一个关键词排序,后面的数字4表示按照第四列排序;n指代numberic sort,根据其数值排序;r指代reverse,这里是指反向比较结果,输出时默认从小到大,反向后从大到小。)。本例中,可以看到%MEM在第4个位置,根据%MEM的数值进行由大到小的排序。-k3表示按照cpu占用率排序。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具Linux下如何查看哪些进程占用的CPU内存资源最多
linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合:
ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head
linux下获取占用内存资源最多的10个进程,可以使用如下命令组合:
ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head
命令组合解析(针对CPU的,MEN也同样道理):
ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head
该命令组合实际上是下面两句命令:
ps aux|head -1
ps aux|grep -v PID|sort -rn -k +3|head
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。博客分类:
Linux下提供top、ps命令查看当前cpu、mem使用情况,简要介绍如下:一、使用ps查看进程的资源占用ps
-aux查看进程信息时,第三列就是CPU占用。[root@localhost utx86]# ps -aux | grep
my_processWarning: bad syntax, perhaps a bogus '-'? See
/usr/share/doc/procps-3.2.7/FAQroot
0:05 ./my_processroot
my_process每一列含义如下USER
COMMAND即my_process进程当前占用cpu 3.4%,
内存0.9%二、top动态查看系统负荷top -n 1显示后退出[root@localhost
utx86]# top -n 1top - 14:23:20 up
5:14, 14 users,
load average: 0.00,
0.04, 0.01Tasks: 183 total,
1 running, 181 sleeping,
1 stopped,
zombieCpu(s):
0.0%ni, 95.8%id,
0.0%stMem:
2066240k total,
1507316k used,
558924k free,
190472k buffersSwap:
2031608k total,
2031520k free,
1087184k cached1、获取cpu占用情况[root@localhost utx86]# top -n 1 |grep
CpuCpu(s):
0.0%ni, 95.9%id,
0.0%st解释:1.9%us是用户占用cpu情况1.3%sy,是系统占用cpu情况得到具体列的值:[root@localhost
utx86]# top -n 1 |grep Cpu | cut -d "," -f 1 | cut -d ":" -f
21.9%us[root@localhost utx86]# top -n 1 |grep Cpu | cut -d "," -f
21.3%sy2、获得内存占用情况[root@localhost utx86]# top -n 1 |grep
2066240k total,
1515784k used,
550456k free,
buffers获得内存情况指定列[root@localhost c++_zp]# top -n 1 |grep Mem |
cut -d "," -f 1 | cut -d ":" -f 22066240k total[root@localhost c++_zp]#
top -n 1 |grep Mem | cut -d "," -f 21585676k
used三、编程实现现在可以通过程序将cpu使用率、内存使用情况保存到文件中//
test.cpp#include &stdio.h&#include &unistd.h&#include
&stdlib.h&int main(){system("top -n 1 |grep Cpu | cut -d \",\"
-f 1 | cut -d \":\" -f 2 &cpu.txt");system("top -n 1 |grep Cpu | cut -d
\",\" -f 2 &&cpu.txt");system("top -n 1 |grep Mem | cut -d \",\" -f 1
| cut -d \":\" -f 2 &&cpu.txt");system("top -n 1 |grep Mem | cut -d
\",\" -f 2 &&cpu.txt");return
0;}编译、运行:[root@localhost study]# g++
test.cpp[root@localhost study]# ./a.out[root@localhost study]# cat
cpu.txt2.1%us1.5%sy2066240k total1619784k used四、硬盘使用率编程实现
1.硬盘使用率 命令df -lh
2.程序实现,调用statfs
int statfs(const char *path, struct statfs *buf); int fstatfs(int fd,
struct statfs *buf); struct statfs { long
/* type of
filesystem (see below) */ long
/* optimal transfer block size
/* total data blocks in file system */
/* free blocks in fs */ long
blocks avail to non-superuser */ long
/* total file nodes in
file system */ long
/* free file nodes in fs */ fsid_t
/* file system id */ long
/* maximum length of
filenames */ };
int fstatvfs(int fildes, struct statvfs *buf); int statvfs(const char
*restrict path, struct statvfs *restrict buf);
struct statvfs { unsigned long
/* file system block size */
unsigned long
/* fragment size */ fsblkcnt_t
/* size of fs in f_frsize units */ fsblkcnt_t
blocks */ fsblkcnt_t
/* # free blocks for non-root */
fsfilcnt_t
/* # inodes */ fsfilcnt_t
free inodes */ fsfilcnt_t
/* # free inodes for non-root */
unsigned long
/* file system id */ unsigned long
/* mount flags */ unsigned long
/* maximum filename length */
#include &sys/vfs.h& #include &sys/statvfs.h&
#include &string.h& #include &stdlib.h& #include
&stdio.h& int gethd(char *path); int main() { char
buf[256],* FILE * while(1) {
file=fopen("/etc/fstab","r"); if(!file)
memset(buf,0,sizeof(buf)); while(fgets(buf,sizeof(buf),file)) {
ptr=strtok(buf," "); if(ptr&&((strncmp(ptr,"/dev",4)==0))) {
ptr=strtok(NULL," "); gethd(ptr); } } fclose(file);
sleep(2); } }
int gethd(char *path) { struct statvfs stat1;
statvfs(path,&stat1); if(stat1.f_flag) printf("%s total=%dK
free=%dK %0.1f%%
\n",path,stat1.f_bsize*stat1.f_blocks/1024,stat1.f_bsize*stat1.f_bfree/1024,
((float)stat1.f_blocks-(float)stat1.f_bfree)/(float)stat1.f_blocks*100);
import java.io.*;
/** * linux 下cpu 内存 磁盘 jvm的使用监控 * @author avery_leo * */
public class TT {
/** * 获取cpu使用情况 * @return * @throws
Exception */ public double getCpuUsage() throws Exception {
cpuUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b
-n 1");// 调用系统的“top"命令
BufferedReader in =
BufferedReader(new InputStreamReader(p.getInputStream()));
String str =
String[] strArray =
while ((str = in.readLine()) !=
int m = 0;
if (str.indexOf(" R ") != -1) {//
只分析正在运行的进程,top进程本身除外 &&
strArray = str.split(" ");
(String tmp : strArray) {
if (tmp.trim().length() == 0)
if (++m == 9) {// 第9列为CPU的使用百分比(RedHat
cpuUsed +=
Double.parseDouble(tmp);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
return cpuU
/** * 内存监控 * @return * @throws
Exception */ public double getMemUsage() throws Exception {
double menUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令
BufferedReader in =
BufferedReader(new InputStreamReader(p.getInputStream()));
String str =
String[] strArray =
while ((str = in.readLine()) !=
int m = 0;
if (str.indexOf(" R ") != -1) {//
只分析正在运行的进程,top进程本身除外 &&
System.out.println("------------------3-----------------");
strArray =
str.split(" ");
for (String tmp : strArray) {
(tmp.trim().length() == 0)
if (++m == 10) {
// 9)--第10列为mem的使用百分比(RedHat
menUsed += Double.parseDouble(tmp);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
return menU
/** * 获取磁盘空间大小 *
* @return * @throws Exception */
public double getDeskUsage() throws Exception {
double totalHD = 0;
double usedHD = 0;
Runtime rt = Runtime.getRuntime();
= rt.exec("df -hl");//df -hl 查看硬盘空间
BufferedReader in =
BufferedReader(new InputStreamReader(p.getInputStream()));
String str =
String[] strArray =
int flag = 0;
while ((str =
in.readLine()) != null) {
int m = 0;
if (flag &
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
System.out.println("----tmp----" + tmp);
if (tmp.indexOf("G") != -1) {
if (m == 2) {
System.out.println("---G----" + tmp);
if (!tmp.equals("") &&
!tmp.equals("0"))
totalHD += Double.parseDouble(tmp
.substring(0,
tmp.length() - 1)) * 1024;
if (m == 3) {
System.out.println("---G----" + tmp);
if (!tmp.equals("none") && !tmp.equals("0"))
Double.parseDouble(tmp.substring(
0, tmp.length() - 1)) * 1024;
if (tmp.indexOf("M") != -1) {
if (m == 2) {
System.out.println("---M---" + tmp);
if (!tmp.equals("") &&
!tmp.equals("0"))
totalHD += Double.parseDouble(tmp
.substring(0,
tmp.length() - 1));
if (m == 3) {
System.out.println("---M---" + tmp);
if (!tmp.equals("none") && !tmp.equals("0"))
Double.parseDouble(tmp.substring(
0, tmp.length() - 1));
System.out.println("----3----" + usedHD);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
return (usedHD / totalHD) * 100;
public static void main(String[] args) throws Exception {
System.out.println("---------------cpu used:" +
cpu.getCpuUsage() + "%");
System.out.println("---------------mem used:" +
cpu.getMemUsage() + "%");
System.out.println("---------------HD used:" +
cpu.getDeskUsage() + "%");
System.out.println("------------jvm监控----------------------");
lRuntime = Runtime.getRuntime();
System.out.println("--------------Free
Momery:" + lRuntime.freeMemory()+"K");
System.out.println("--------------Max Momery:" +
lRuntime.maxMemory()+"K");
System.out.println("--------------Total
Momery:" + lRuntime.totalMemory()+"K");
System.out.println("---------------Available Processors :" +
lRuntime.availableProcessors());
监视磁盘hda1#!/bin/sh# disk_mon#
monitor the disk space# get percent column and strip off header
row from dfLOOK_OUT=0until [ "$LOOK_OUT" -gt
LOOK_OUT=`df | grep /hda1 | awk
'{print $5}' | sed 's/%//g'`
echo $LOOK_OUT%
sleep 1doneecho "Disk hda1 is nearly
hdtest.sh#!/bin/ksh#检测硬盘剩余空间并警告的shell&nbspV050921#简单说明: 可由root用户将此脚本加入crontab,启动时间一般最好设为每天营业前,当此脚本启动时如检测到已用硬盘空间超过指定范围,则将hdwarning.sh脚本拷贝到指定用户根目录下;否则将删除指定用户的目录下的hdwarning.sh脚本.usedhd=80
#自定义超限已用硬盘空间大小比例,默认为80%test "$1" &&&nbspuserdir=$1 ||&nbspuserdir=/usr/scabs
#前台用户的目录(默认设为统版用户),也可在调用此脚本时加上指定前台用户的目录参数hdwarning=$(df&nbsp-v |sed '1d;s/.$//;s/\/dev\///'|awk '$6&'"$usedhd"' {print $2," = ",$6"%"}')test "$hdwarning" && {&nbspcp /usr/bin/hdwarning.sh ${userdir}/hdwarning.sh
\& ${userdir}/hdwarning.log &nbspchmod&nbsp777 ${userdir}/hdwarning.sh ${userdir}/hdwarning.log
} \|| {&nbsprm ${userdir}/hdwarning.sh&nbsp2&/dev/null
\mv ${userdir}/hdwarning.log ${userdir}/hdwarning.log.bak&nbsp2&/dev/null
}hdwarning.sh#!/bin/ksh#检测硬盘剩余空间并警告的shell&nbspV050921#增加当超标时,只在预先指定的前N位预先的指定用户登录时才显示提示信息,#即只有这前面N位用户才有可能及时反馈,避免当超标时接到过多的前台反馈电话&nbspV050923#请先编辑指定用户根下的&nbsp.profile ,在最后追加一行# &nbsptest&nbsp-x&nbsphdwarning.sh && &nbsp./hdwarning.sh#若.profile最后已加入了自启动专用程序命令行,则请在此行前面插入上述行#简单说明: 当指定用户登录后,若当前目录中hdwarning.sh脚本存在(一般此#时硬盘已用空间已经超标),则运行此脚本,并在屏幕显示警告信息,此时终端#操作人员应该及时将此信息把馈给预先指定的部门或预先指定的管理人员,#以便作相应的处理.若未超标或已清理磁盘文件并达标,则将删除脚本自身#hdwarning.sh(取消登录时的检测和警告信息)usedhd=80
#自定义超限已用硬盘空间大小比例,默认为80%loginnum=10
#自定义最初登录反馈的用户数,默认为前&nbsp10 位name="运维部"
#接受反馈的部门或管理人员
tel="2113714&nbsp2110394"
#接受反馈的部门或管理人员的联系方式或电话test "$1" &&&nbspuserdir=$1 ||&nbspuserdir=/usr/scabs
#前台用户的目录(默认设为统版用户),也可在调用此#脚本时加上指定前台用户的目录参数hdwaring(){&nbspttyname=$(tty)echo ${ttyname##*
cpu====================================================================:
/proc目路下的内存文件系统映射了系统的运行时的一些信息,包括进程列表,内存信息,CPU使用情况,还有网络等等所以可以通过读/proc下的文件来实现统计信息的获取但是,要注意的时不同的版本,将/proc下的每个文件中的类容会有一些差别,每一个项代表什么要自己分析,最好根据top的输出去分析然后就可以通过shell教本或者C取得CPU使用率比如:我的机子是AS4(Kernel
2.6.9-5)cat /proc/statcpu
61338cpu0 0 839 0 49564cpu1 8
91cpu2 4 985 2225180
909 2839cpu3 7 888 3intr
720 0 12 12 0 7 2 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 30 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0ctxt btime
processes 603543procs_running 1procs_blocked
0然后就可以自己计算了
#########GetCPU.sh
######Author:duanjigang
while true
'$1=="cpu"{Total=$2+$3+$4+$5+$6+$7;print "Free: " $5/Total*100"%
" " Used: " (Total-$5)*100/Total"%"}' &/proc/stat
#./GetCPU.shFree:
99.4532% Used: 0.546814%Free: 99.4532% Used: 0.546813%Free: 99.4532%
Used: 0.546813%Free: 99.4532% Used: 0.546813%这样应该可以的
MEM====================================================================:
(1):取CPU使用率机器:LinuxAS4
2.6.9-5.ELsmp (不通版本的内核会有差异的)
#cpu.sh-to get the utilization of every cpu
#author:duanjigang&&
awk '$0 ~/cpu[0-9]/' /proc/stat
| while read line
echo "$line" | awk '{total=$2+$3+$4+$5+$6+$7+$8;free=$5;\
print$1" Free "free/total*100"%",\
"Used " (total-free)/total*100"%"}'
cpu.sh#./cpu.shcpu0 Free 99.7804% Used 0.219622%cpu1 Free 99.8515%
Used 0.148521%cpu2 Free 99.6632% Used 0.336765%cpu3 Free 99.6241% Used
0.375855%(2)网络流量情况
#if.sh-to get the network flow of each interface
#for my beloved ning
#author:duanjigang&&
echo "name
' NR&2' /proc/net/dev
| while read line
echo "$line" | awk -F ':' '{print "
awk '{print $1"
#./if.shname
PackTranlo
991835eth1
0(3):端口情况http://bbs.chinaunix.net/viewthread.php?tid=864757&highlight=duanjigang(4)至于内存cat
/proc/meminfo | grep "MemTotal"cat /rpco/meninfo
"MemFree"就可以了吧
浏览 23031
浏览: 67618 次
来自: 广州
fsdafsdaaaaaaaaaaaaaaaaaaaaaaaa ...
谢谢啦,真心觉得读取配置文件要向jar包那么方便就好了
javasem# 写道求楼主QQ 大家聊几句呵呵,说明来意先。 ...
求楼主QQ 大家聊几句
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 unix查看进程占用内存 的文章

 

随机推荐