
目录一. 简介二. 示例2.1 ps -ef ⇒ 显示所有进程的详细信息2.2 ps -ef | grep nginx ⇒ 显示nginx相关的所有进程的详细信息2.3 ps -p PID ⇒ 显示指定PID的进程信息2.4 ps aux --sort-%cpu ⇒ 按CPU使用量排序2.5 ps aux --sort-%mem ⇒ 按内存使用量排序三. 过滤grep自身进程一. 简介用于显示系统中当前运行的进程信息。提供了查看进程的不同视图和选项允许用户了解系统上正在运行的进程的状态、资源使用情况等。-e显示所有进程而不仅仅是与当前终端关联的进程。-f以完整的格式显示进程信息包括进程的详细信息如进程的状态、PID进程ID、PPID父进程ID、CCPU利用率、STIME启动时间、TTY终端类型、TIME执行的累计CPU时间、CMD进程的命令等。-a显示其他用户拥有的进程。例如你当前用户是spluser如果没有-a通常只显示与当前用户相关的进程加上-a后可以看到其他用户的进程。-u以用户友好的格式显示把进程显示成比较容易阅读的形式。-u⇒user-oriented format。x显示没有终端的进程Linux 中很多后台服务没有 TTY/终端如果没有x这类进程可能不会显示。二. 示例2.1ps -ef⇒ 显示所有进程的详细信息2.2ps -ef | grep nginx⇒ 显示nginx相关的所有进程的详细信息2.3ps -p PID⇒ 显示指定PID的进程信息2.4ps aux --sort-%cpu⇒ 按CPU使用量排序apluserubuntu22-1:~$psaux--sort-%cpu|headUSERPID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDgit108211.711.22930332908112? Sl Aug1398:32 sidekiq7.3.9 queues:default,mailers[0of20busy]git2460683.811.42256424921792? Sl 08:380:19 puma: cluster worker2:719[gitlab-puma-worker]git2460593.411.62273384941492? Sl 08:380:17 puma: cluster worker0:719[gitlab-puma-worker]git2460633.411.32260456919992? Sl 08:380:17 puma: cluster worker1:719[gitlab-puma-worker]git2460723.411.42267048927324? Sl 08:380:17 puma: cluster worker3:719[gitlab-puma-worker]git2460763.411.72292136950492? Sl 08:380:17 puma: cluster worker5:719[gitlab-puma-worker]git2382031.812.32449988997188? Sl 08:110:39 puma: cluster worker4:719[gitlab-puma-worker]gitlab-11911.11.0221281282064? Ss Aug139:39 postgres: gitlab-psql gitlabhq_production[local]idle gitlab-7401.00.3207901229016? Ssl Aug139:10 /opt/gitlab/embedded/bin/postgres_exporter --no-collector.stat_user_tables--collector.postmaster--web.listen-addresslocalhost:9187 --extend.query-path/var/opt/gitlab/postgres-exporter/queries.yaml apluserubuntu22-1:~$2.5ps aux --sort-%mem⇒ 按内存使用量排序apluserubuntu22-1:~$psaux--sort-%mem|headUSERPID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDgit2382031.612.32469444994108? Sl 08:110:41 puma: cluster worker4:719[gitlab-puma-worker]git2460682.311.92332712967744? Sl 08:380:21 puma: cluster worker2:719[gitlab-puma-worker]git2460762.011.92316712966892? Sl 08:380:19 puma: cluster worker5:719[gitlab-puma-worker]git2460722.111.82311080954420? Sl 08:380:19 puma: cluster worker3:719[gitlab-puma-worker]git2460592.211.72342312949336? Sl 08:380:20 puma: cluster worker0:719[gitlab-puma-worker]git2460632.011.62286760938596? Sl 08:380:19 puma: cluster worker1:719[gitlab-puma-worker]git108211.711.22930332910068? Sl Aug1399:22 sidekiq7.3.9 queues:default,mailers[0of20busy]git7190.26.72064596548916? Ssl Aug132:18 puma7.2.0(unix:///var/opt/gitlab/gitlab-rails/sockets/gitlab.socket,tcp://127.0.0.1:8080)[gitlab-puma-worker]jenkins3200.34.17860116331548? Ssl Aug133:20 /usr/bin/java-Djava.awt.headlesstrue-jar/usr/share/java/jenkins.war--webroot/var/cache/jenkins/war--httpPort8082apluserubuntu22-1:~$三. 过滤grep自身进程⏹我们在查询进程时往往会使用grep命令进一步缩小范围但是此时往往会将grep自身的进程也显示出来grep --colorauto sshdgrep命令自身的进程apluserubuntu24-01:~$ps-ef|grepsshdroot2044510Dec28 ? 00:00:00 sshd: /usr/sbin/sshd-D[listener]0of10-100 startups root3897920445001:47 ? 00:00:00 sshd: apluser[priv]apluser3908038979001:47 ? 00:00:00 sshd: aplusernotty root3961220445005:03 ? 00:00:00 sshd: apluser[priv]apluser3971139612005:03 ? 00:00:00 sshd: apluserpts/0 apluser3980139712005:39 pts/0 00:00:00grep--colorauto sshd⏹可通过下面这两种方式过滤grep自身的进程ps -ef | grep sshd | grep -v grep从查询结果中过滤掉grep关键词。ps -ef | grep [s]shd[s]shd的正则表达式仍然匹配sshd但因为 grep 自身的命令行是grep [s]shd而不是sshd不会匹配到自身的进程。