26 Dec 2014
作用:查看系统进程
语法:
ps -u username 查看特定用户的所有进程ps aux 查看完整的系统运行的进程,a列出所有,u按用户分类,x列出进程相关控制台与终端ps -elf 以长格式查看系统进程信息,e列出所有,l以长格式显示,f全格式显示ps aux列标题含义:
PID process id,进程唯一标识可用于终止进程, kill -9 pidSTAT 表示进程的状态D 不能中断的进程(通常为IO)R running正在运行的进程S stoped已经中断的进程T 已经停止或者暂停的进程,如果我们正在运行一个命令,比如说 sleep 10 如果我们按一下ctrl -z 让他暂停,那么我们用ps查看就会显示T这个状态X 已经死掉的进程(这个从来不会出现)Z 僵尸进程,杀不掉的垃圾进程,占系统一小点资源,不过没有关系。如果太多,就有问题了。< 高优先级进程N 低优先级进程L 在内存中被锁了内存分页s 主进程l 多线程进程+ 在前台的进程# 查看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
作用:根据名称或其他属性查看进程pid
语法:pgrep 参数 pattern
参数:
-d 间隔符, 进程号间隔符,默认是换行符-f, 默认pattern匹配进程名称,加上-f匹配整个命令行-U 用户名称/uid, 指定某个用户执行的进程-G 组名/gid, 指定某个组执行的进程-o, 只匹配存在时间最久的进程用法举例:
# 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”参数,匹配最老的一个
作用:查看正在运行的程序或脚本的pid
语法:pid 参数 程序名称
用法示例:
ps aux |grep mysql|grep -v root|awk '{print $2}' 9973 10096 pidof mysqld 10096 9973