17.5.0: 系统管理-ps



1. 命令:ps

作用:查看系统进程
语法:

ps aux列标题含义:

# 查看rsz实际内存占用,用来看哪个进程占用的内存多
ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid'
PID COMMAND         COMMAND                     %CPU   RSZ    VSZ STIME USER       UID
    1 init            /sbin/init                   0.0   984  19232  2015 root         0
    2 kthreadd        [kthreadd]                   0.0     0      0  2015 root         0
    3 migration/0     [migration/0]                0.0     0      0  2015 root         0
    4 ksoftirqd/0     [ksoftirqd/0]                0.0     0      0  2015 root         0
    5 migration/0     [migration/0]                0.0     0      0  2015 root         0
    6 watchdog/0      [watchdog/0]                 0.0     0      0  2015 root         0
    7 migration/1     [migration/1]                0.0     0      0  2015 root         0
  ...
  505 jbd2/dm-0-8     [jbd2/dm-0-8]                0.0     0      0  2015 root         0
  506 ext4-dio-unwrit [ext4-dio-unwrit]            0.0     0      0  2015 root         0
  578 udevd           /sbin/udevd -d               0.0   408  10880  2015 root         0
  806 edac-poller     [edac-poller]                0.0     0      0  2015 root         0
  931 kdmflush        [kdmflush]                   0.0     0      0  2015 root         0
  970 jbd2/sda1-8     [jbd2/sda1-8]                0.0     0      0  2015 root         0
  971 ext4-dio-unwrit [ext4-dio-unwrit]            0.0     0      0  2015 root         0
  972 jbd2/dm-2-8     [jbd2/dm-2-8]                0.0     0      0  2015 root         0
  973 ext4-dio-unwrit [ext4-dio-unwrit]            0.0     0      0  2015 root         0
 1006 kauditd         [kauditd]                    0.0     0      0  2015 root         0
 1184 flush-253:0     [flush-253:0]                0.0     0      0  2015 root         0
 1344 auditd          auditd                       0.0   732  93200  2015 root         0
 1360 rsyslogd        /sbin/rsyslogd -i /var/run/  0.0  4276 251964  2015 root         0
 1401 sshd            /usr/sbin/sshd               0.0   628  66648  2015 root         0
 1477 master          /usr/libexec/postfix/master  0.0  2556  81292  2015 root         0
 1485 crond           crond                        0.0   728 117296  2015 root         0
 1492 qmgr            qmgr -l -t fifo -u           0.0  2668  81540  2015 postfix     89
 1500 mingetty        /sbin/mingetty /dev/tty1     0.0   504   4064  2015 root         0
 1502 mingetty        /sbin/mingetty /dev/tty2     0.0   504   4064  2015 root         0
 1504 mingetty        /sbin/mingetty /dev/tty3     0.0   504   4064  2015 root         0
 1506 mingetty        /sbin/mingetty /dev/tty4     0.0   504   4064  2015 root         0
 1508 mingetty        /sbin/mingetty /dev/tty5     0.0   504   4064  2015 root         0
 1510 mingetty        /sbin/mingetty /dev/tty6     0.0   504   4064  2015 root         0
 1511 udevd           /sbin/udevd -d               0.0   452  10876  2015 root         0
 1512 udevd           /sbin/udevd -d               0.0   236  10876  2015 root         0
 6695 pickup          pickup -l -t fifo -u         0.0  3364  81372 11:37 postfix     89
 7363 flush-253:2     [flush-253:2]                0.0     0      0 Dec21 root         0
 7537 sshd            sshd: root@pts/1             0.0  4180 100408 12:41 root         0
 7541 bash            -bash                        0.0  1960 108376 12:41 root         0
 7609 mysqld_safe     /bin/sh ./bin/mysqld_safe -  0.0  1484 106064 12:46 root         0
 7651 mysqld          /home/live800/working/mysql  2.7 1922380 13338652 12:46 root     0
 7776 sshd            sshd: root@pts/0             0.0  4236 100408 12:52 root         0
 7778 bash            -bash                        0.0  1960 108304 12:52 root         0
 7882 sshd            sshd: root [priv]            0.7  4128  99308 13:00 root         0
 7883 sshd            sshd: root [net]             0.0  1660  67992 13:00 sshd        74
 7884 ps              ps -e -o pid,comm,args,pcpu  1.0  1168 110236 13:00 root         0

2. 命令:pgrep

作用:根据名称或其他属性查看进程pid
语法:pgrep 参数 pattern
参数:

用法举例:

# mysql有两个实例,每个实例两个进程,分别为root和mysql执行
ps aux|grep mysql|grep -v grep|awk '{print $1"-"$2}'
root-9858
mysql-9973
root-9981
mysql-10096

# pgrep使用进程名称查找进程号,注意必须使用进程名称mysqld
pgrep mysqld
9858
9973
9981
10096

# -d指定间隔符
pgrep -d "-" mysqld
9858-9973-9981-10096

# -f用来在执行命令全局中查找关键字
pgrep -f my
9858
9973
9981
10096

pgrep -f 330
9858
9973
9981
10096

# -f和-U搭配找出3306端口号实例的root/mysql执行的进程
pgrep -f 3306 -U root
9858

pgrep -f 3306 -U mysql
9973

PS:特别需要注意
有时候你执行的脚本有可能被上面的-f参数模糊匹配到
例如我在做一个mysql的启动脚本时,脚本名称是mysqld,
当我在这个脚本中匹配mysql的运行实例,并杀死它时,经常脚本会莫名奇妙的中止
后来才发现原来是-f匹配mysql实例时把mysqld脚本自身也匹配到了

解决办法就是使用”-o”参数,匹配最老的一个


3. 命令:pidof

作用:查看正在运行的程序或脚本的pid
语法:pid 参数 程序名称

用法示例:

ps aux |grep mysql|grep -v root|awk '{print $2}'
9973
10096

pidof mysqld
10096 9973