您好,登錄后才能下訂單哦!
本篇文章為大家展示了Linux中顯示內存和CPU使用率最高的進程是怎么樣的及SHELL腳本怎么編寫,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
顯示CPU占用率最高的十個進程信息
# ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 30222 5.0 3.8 10685936 38228 ? Sl Apr24 1453:59 ./pd-server -data-dir=/data/tidb/pd -log-file=/data/tidb/log/pd.log -name=pd1
root 23484 2.4 14.7 779468 147244 ? Sl Apr24 701:46 ./tidb-server -L info -store=tikv --path=/data/tidb/tidb --path=127.0.0.1:2379 -log-file=/data/tidb/log/tidb.log
root 23415 2.0 10.1 1359044 101332 ? S<l Apr24 597:36 ./tikv-server --pd=127.0.0.1:2379 --data-dir=/data/tidb/tikv --log-file=/data/tidb/log/tikv.log
root 9092 0.2 0.1 158240 1972 ? Ssl Mar29 150:51 redis-server 0.0.0.0:6379
root 4072 0.1 0.4 110676 4204 pts/2 Ss 10:16 0:00 -bash
mysql 27252 0.1 16.1 6842120 160408 ? Sl Apr25 50:53 /usr/local/mysql-5.7.25/bin/mysqld --basedir=/usr/local/mysql-5.7.25 --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql-5.7.25/lib/plugin --user=mysql --log-error=/data/mysql/log/mysqld.err --open-files-limit=81920 --pid-file=/data/mysql/mysqld.pid --socket=/tmp/mysql.sock --port=3306
rpcuser 3805 0.0 0.0 28372 8 ? Ss Mar12 0:00 rpc.statd --no-notify
rpc 3798 0.0 0.0 18988 100 ? Ss Mar12 0:13 rpcbind
root 95 0.0 0.0 0 0 ? S Mar12 0:00 [deferwq]
root 944 0.0 0.0 0 0 ? S Mar12 0:24 [kauditd]
顯示系統消耗內存資源最高的四個進程
# ps aux | grep -v "USER" | sort -rn -k +4 | head -4 | awk -F ' ' '{print $11,$4}'
/usr/local/mysql-5.7.25/bin/mysqld 16.1
./tidb-server 14.7
./tikv-server 10.1
./pd-server 3.8
虛擬內存使用最多的前10個進程--詳細
# ps auxw|head -1;ps auxw|sort -rn -k5|head -10
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
mysql 23730 73.1 82.4 29577272 27209640 ? Sl Apr30 15657:26 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysqllog/error.log --open-files-limit=65535 --pid-file=/data/mysql/mysql-zhsq-ref1-168.pid --socket=/var/lib/mysql/mysql.sock
root 1285 0.0 0.0 255616 5456 ? Sl Mar07 10:37 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
root 3228 0.0 0.0 155444 928 pts/0 S+ 09:01 0:00 sort -rn -k5
root 3783 0.4 0.0 123720 7816 ? S<sl Apr26 126:21 /usr/local/aegis/aegis_client/aegis_10_63/AliYunDun
root 1784 0.0 0.0 116916 1280 ? Ss Mar07 0:13 crond
root 3227 0.0 0.0 110240 1172 pts/0 R+ 09:01 0:00 ps auxw
root 2753 0.0 0.0 108320 1984 pts/0 Ss 08:50 0:00 -bash
root 2992 0.0 0.0 108316 1956 pts/1 Ss+ 08:55 0:00 -bash
root 16579 0.0 0.0 108312 1752 ? Ss Apr24 0:00 -bash
root 2751 0.0 0.0 102552 4508 ? Ss 08:50 0:00 sshd: root@pts/0,pts/1
顯示內存按大小排序:
# ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' |cut -d "" -f2 | cut -d "-" -f1
0.00 Mb COMMAND
29381.29 Mb /usr/local/jdk/bin/java
2294.26 Mb /usr/local/cloudmonitor/jre/bin/java
222.86 Mb /sbin/rsyslogd
32.23 Mb /usr/local/aegis/aegis_client/aegis_10_65/AliYunDun
10.67 Mb /usr/local/cloudmonitor/wrapper/bin/./wrapper /usr/local/cloudmonitor/wrapper/bin/../conf/wrapper.conf wrapper.syslog.ident=cloudmonitor wrapper.pidfile=/usr/local/cloudmonitor/wrapper/bin/./cloudmonitor.pid wrapper.daemonize=TRUE wrapper.name=cloudmonitor wrapper.displayname=cloudmonitor wrapper.statusfile=/usr/local/cloudmonitor/wrapper/bin/./cloudmonitor.status wrapper.java.statusfile=/usr/local/cloudmonitor/wrapper/bin/./cloudmonitor.java.status wrapper.lockfile=/var/lock/subsys/cloudmonitor wrapper.script.version=3.5.27
10.30 Mb auditd
4.57 Mb /usr/local/aegis/aegis_update/AliYunDunUpdate
1.40 Mb crond
1.16 Mb ps
0.78 Mb sshd: root@pts/0
0.68 Mb /sbin/udevd
0.68 Mb /sbin/udevd
0.68 Mb /sbin/udevd
0.67 Mb qmgr
0.58 Mb /sbin/dhclient
0.58 Mb /sbin/dhclient
0.58 Mb /usr/sbin/sshd
0.58 Mb /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
0.57 Mb /usr/sbin/zabbix_agentd
.................................................
# ps aux | awk '{print $6/1024 " MB\t\t" $11}' | sort -n
# ps aux | awk '{print $6/1024 " MB\t\t" $11}' | sort -n
0 MB [aio/0]
0 MB [aio/1]
0 MB [aio/2]
0 MB [aio/3]
0 MB [async/mgr]
0 MB [ata_aux]
0 MB [ata_sff/0]
0 MB [ata_sff/1]
0 MB [ata_sff/2]
0 MB [ata_sff/3]
0 MB [bdi-default]
0 MB [cgroup]
0 MB COMMAND
0 MB [cqueue]
0 MB [crypto/0]
0 MB [crypto/1]
0 MB [crypto/2]
....................................................
....................................................
....................................................
....................................................
1.51172 MB qmgr
1.80078 MB -bash
2.34766 MB pickup
2.58594 MB /usr/local/aegis/aegis_update/AliYunDunUpdate
4.37891 MB sshd:
5.02734 MB /usr/local/aegis/aegis_client/aegis_10_65/AliYunDun
6.8125 MB /sbin/rsyslogd
63.2031 MB /usr/local/cloudmonitor/jre/bin/java
19845 MB /usr/local/jdk/bin/java
shell腳本一例:
# vim mem.sh
#!/bin/bash
date=`date "+%Y-%m-%d %H:%M:%S"`
#顯示消耗資源內存最高的進程名
first=`ps aux | grep -v "grep" | grep -v "USER" | sort -rn -k +4 | head -4 | awk -F ' ' '{print $11}' | sed -n 1p`
Second=`ps aux | grep -v "grep" | grep -v "USER" | sort -rn -k +4 | head -4 | awk -F ' ' '{print $11}' | sed -n 2p`
Third=`ps aux | grep -v "grep" | grep -v "USER" | sort -rn -k +4 | head -4 | awk -F ' ' '{print $11}' | sed -n 3p`
Fourth=`ps aux | grep -v "grep" | grep -v "USER" | sort -rn -k +4 | head -4 | awk -F ' ' '{print $11}' | sed -n 4p`
#顯示消耗內存資源最高進程、內存使用率
one=`ps aux | grep -v "grep" | grep -v "USER" | grep "$first" | awk -F ' ' '{print $4}'`
two=`ps aux | grep -v "grep" | grep -v "USER" | grep "$Second" | awk -F ' ' '{print $4}'`
three=`ps aux | grep -v "grep" | grep -v "USER" | grep "$Third" | awk -F ' ' '{print $4}'`
four=`ps aux | grep -v "grep" | grep -v "USER" | grep "$Fourth" | awk -F ' ' '{print $4}'`
#使用echo顯示結果
echo "$date $first $one"
echo "$date $Second $two"
echo "$date $Third $three"
echo "$date $Fourth $four"
運行結果展示:
# ./mem.sh
2019-05-14 10:23:00 /usr/local/mysql-5.7.25/bin/mysqld 0.0
16.1
2019-05-14 10:23:00 ./tidb-server 14.7
2019-05-14 10:23:00 ./tikv-server 10.1
2019-05-14 10:23:00 ./pd-server 3.8
上述內容就是Linux中顯示內存和CPU使用率最高的進程是怎么樣的及SHELL腳本怎么編寫,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。