常用shell 命令整理 一 进程 cpu

时间:2023-03-10 01:41:49
常用shell 命令整理 一 进程 cpu

1.查看内存从大到小排列

ps -e -o "%C : %p : %z : %a"|sort -k5 -nr

分析:

-e 显示进程

-o 按用户自定义格式显示

%C cpu

%p 父进程id

%z 虚拟内存

%a

sort 排序命令

-k5 按第5列排序

-nr 比较数字 从大大小

-n  比较数字 从小到大

实例 1.1: 按内存排序从大到小,显示前五行

 [devtac@test_1 ~]$ ps -e -o "%C : %p : %z : %a"|sort -k5 -nr |head -
0.0 : : : /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/test_1.smartpay.com.cn.err --pid-file=/usr/local/mysql/data/test_1.smartpay.com.cn.pid
0.0 : : : /usr/sbin/console-kit-daemon --no-daemon
0.0 : : : /usr/bin/pulseaudio --start --log-target=syslog
0.0 : : : /usr/libexec/gdm-simple-greeter
0.0 : : : automount --pid-file /var/run/autofs.pid

实例 1.2 按进程id 从小到大

[devtac@test_1 ~]$ ps -e -o "%C : %p : %z : %a"|sort -k3 -n |head -
%CPU : PID : VSZ : COMMAND
0.0 : : : /sbin/init
0.0 : : : [kthreadd]
0.0 : : : [migration/]
0.0 : : : [ksoftirqd/]

实例 1.3 按cpu 利用率 从大到小

[devtac@test_1 ~]$ ps -e -o "%C : %p : %z : %a"|sort -k1 -nr | head -
0.1 : : : smbd -D
%CPU : PID : VSZ : COMMAND
0.0 : : : [ksoftirqd/]
0.0 : : : [kstriped]
0.0 : : : [migration/]

实例1.4 某个进程在哪个cpu 上运行

ps -eo pid,args,psr

[devtac@test_1 ~]$ ps -eo pid,args,psr | head -
PID COMMAND PSR
/sbin/init
[kthreadd]
[migration/]
[ksoftirqd/]

2 查看http的并发请求数及其连接状态

[root@test_1 Action]# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}'
ESTABLISHED
[root@test_1 Action]#
$NF 这个值取得是ESTABLISHED 没看懂。现在懂了
[root@test_1 Action]# netstat -n | awk '/^tcp/ {print $NF}'
ESTABLISHED
ESTABLISHED
ESTABLISHED
[root@test_1 Action]# netstat -n | awk '/^tcp/ {print NF}' [root@test_1 Action]#
NF 是浏览记录的域的个数,而$NF 则是取得那一个最后一个域。
awk '/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}'
含义就是过了出tcp 开头的,然后以最后一个域分类,并计算每个分类有多少个,

3 查看某个目录文件夹大小,按从大到小排序
du -chs * | sort -rn | head -n 10
[root@test_1 var]# du -chs * | sort -rn | head -n
527M src
292K run
225M lib
130M cache
104K spool
39M log
21G 总用量
20K db
20G file
16K lost+found
[root@test_1 var]# df -h
文件系统 容量 已用 可用 已用%% 挂载点
/dev/mapper/VolGroup-LogVol00
.7G 407M .8G % /
tmpfs 922M 72K 922M % /dev/shm
/dev/sda1 485M 33M 427M % /boot
/dev/mapper/VolGroup-LogVol06
97G .9G 88G % /home
/dev/mapper/VolGroup-LogVol02
68G 197M 64G % /opt
/dev/mapper/VolGroup-LogVol01
15G 165M 14G % /tmp
/dev/mapper/VolGroup-LogVol04
68G .8G 61G % /usr
/dev/mapper/VolGroup-LogVol05
87G .0G 81G % /usr/local
/dev/mapper/VolGroup-LogVol03
77G 21G 53G % /var
4. cpu
4.1 查看cpu 个数
cat /proc/cpuinfo |grep -c processor
[root@test_1 Action]# cat /proc/cpuinfo |grep -c processor

[root@test_1 Action]#

讲解: grep -c 参数

简单翻译:代替输出匹配行内容,而是输出匹配行数

   General Output Control
-c, --count
Suppress normal output; instead print a count of matching lines for each input file. With the -v,
--invert-match option (see below), count non-matching lines. (-c is specified by POSIX.)