Linux中的ps命令是Process Status的缩写。用来列出系统当前运行的那些进程。ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信息,就可以使用top命令。要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程,而 ps 命令就是最基本同时也是非常强大的进程查看命令。使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等等。大部分信息都是可以通过执行该命令得到的。ps为我们提供了进程的一次性的查看,它所提供的查看结果并不动态连续的;如果想对进程时间监控,应该用 top 工具。kill 命令用于杀死进程。
ps [参数]
用来显示当前进程的状态
命令:
ps -A
输出:
ubuntu@VM-4-14-ubuntu:~/cp$ ps -A
PID TTY TIME CMD
1 ? 00:00:01 systemd
2 ? 00:00:00 kthreadd
3 ? 00:00:00 rcu_gp
4 ? 00:00:00 rcu_par_gp
6 ? 00:00:00 kworker/0:0H-kblockd
9 ? 00:00:00 mm_percpu_wq
10 ? 00:00:00 ksoftirqd/0
11 ? 00:00:06 rcu_sched
12 ? 00:00:00 migration/0
13 ? 00:00:00 idle_inject/0
14 ? 00:00:00 cpuhp/0
15 ? 00:00:00 cpuhp/1
16 ? 00:00:00 idle_inject/1
17 ? 00:00:00 migration/1
18 ? 00:00:00 ksoftirqd/1
20 ? 00:00:00 kworker/1:0H-kblockd
21 ? 00:00:00 kdevtmpfs
22 ? 00:00:00 netns
23 ? 00:00:00 rcu_tasks_kthre
24 ? 00:00:00 kauditd
25 ? 00:00:00 khungtaskd
26 ? 00:00:00 oom_reaper
27 ? 00:00:00 writeback
28 ? 00:00:00 kcompactd0
29 ? 00:00:00 ksmd
30 ? 00:00:00 khugepaged
76 ? 00:00:00 kintegrityd
77 ? 00:00:00 kblockd
78 ? 00:00:00 blkcg_punt_bio
80 ? 00:00:00 tpm_dev_wq
...
命令:
ps -u root
ps -u ubuntu
输出:
ubuntu@VM-4-14-ubuntu:~/cp$ ps -u root
PID TTY TIME CMD
1 ? 00:00:01 systemd
2 ? 00:00:00 kthreadd
3 ? 00:00:00 rcu_gp
4 ? 00:00:00 rcu_par_gp
6 ? 00:00:00 kworker/0:0H-kblockd
9 ? 00:00:00 mm_percpu_wq
10 ? 00:00:00 ksoftirqd/0
11 ? 00:00:06 rcu_sched
12 ? 00:00:00 migration/0
13 ? 00:00:00 idle_inject/0
14 ? 00:00:00 cpuhp/0
15 ? 00:00:00 cpuhp/1
16 ? 00:00:00 idle_inject/1
17 ? 00:00:00 migration/1
18 ? 00:00:00 ksoftirqd/1
20 ? 00:00:00 kworker/1:0H-kblockd
21 ? 00:00:00 kdevtmpfs
ubuntu@VM-4-14-ubuntu:~/cp$ ps -u ubuntu
PID TTY TIME CMD
1470 ? 00:00:00 systemd
1471 ? 00:00:00 (sd-pam)
1564 ? 00:00:00 sshd
1565 pts/0 00:00:00 bash
4187 ? 00:00:00 sshd
4193 pts/1 00:00:00 bash
22436 pts/0 00:00:00 tail
233790 pts/0 00:00:00 ps
ubuntu@VM-4-14-ubuntu:~/cp$
命令:
ps -ef
输出:
ubuntu@VM-4-14-ubuntu:~/cp$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jun30 ? 00:00:01 /sbin/init
root 2 0 0 Jun30 ? 00:00:00 [kthreadd]
root 3 2 0 Jun30 ? 00:00:00 [rcu_gp]
root 4 2 0 Jun30 ? 00:00:00 [rcu_par_gp]
root 6 2 0 Jun30 ? 00:00:00 [kworker/0:0H-kblockd]
...
...省略
bind 758 1 0 Jun30 ? 00:00:04 /usr/sbin/named -f -u bind
root 761 1 0 Jun30 ? 00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
syslog 769 1 0 Jun30 ? 00:00:00 /usr/sbin/rsyslogd -n -iNONE
ntp 783 1 0 Jun30 ? 00:00:03 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 112:117
root 789 1 0 Jun30 ? 00:00:00 /lib/systemd/systemd-logind
root 802 1 0 Jun30 ? 00:00:01 /sbin/iscsid
root 803 1 0 Jun30 ? 00:00:00 /sbin/iscsid
root 805 1 0 Jun30 ? 00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root 819 1 0 Jun30 ? 00:02:08 /usr/local/qcloud/tat_agent/tat_agent
root 823 2 0 Jun30 ? 00:00:00 [iscsi_eh]
root 870 1 0 Jun30 ? 00:00:00 /usr/sbin/cron -f
daemon 880 1 0 Jun30 ? 00:00:00 /usr/sbin/atd -f
root 924 1 0 Jun30 ? 00:00:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-sh
root 949 1 0 Jun30 ? 00:00:00 /usr/lib/policykit-1/polkitd --no-debug
root 1065 1 0 Jun30 ttyS0 00:00:00 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyS0 vt220
ubuntu 22436 1565 0 Jun30 pts/0 00:00:00 tail -f test2.txt
root 184293 2 0 10:38 ? 00:00:05 [kworker/1:3-events]
root 204925 2 0 13:00 ? 00:00:00 [kworker/0:1-events]
root 213246 2 0 13:58 ? 00:00:00 [kworker/0:2-events]
root 222303 2 0 15:00 ? 00:00:01 [kworker/1:0-mm_percpu_wq]
root 230256 2 0 15:56 ? 00:00:00 [kworker/u4:2-events_power_efficient]
root 232467 2 0 16:11 ? 00:00:00 [kworker/u4:1-events_power_efficient]
root 233237 2 0 16:16 ? 00:00:00 [kworker/u4:0-events_unbound]
ubuntu 233687 1565 0 16:19 pts/0 00:00:00 ps -ef
命令:
ps -ef|grep ssh
输出:
ubuntu@VM-4-14-ubuntu:~/cp$ ps -ef|grep ssh
root 805 1 0 Jun30 ? 00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root 1417 805 0 Jun30 ? 00:00:00 sshd: ubuntu [priv]
ubuntu 1564 1417 0 Jun30 ? 00:00:00 sshd: ubuntu@pts/0
root 4057 805 0 Jun30 ? 00:00:00 sshd: ubuntu [priv]
ubuntu 4187 4057 0 Jun30 ? 00:00:00 sshd: ubuntu@pts/1
ubuntu 234272 1565 0 16:23 pts/0 00:00:00 grep --color=auto ssh
命令:
ps -l
输出:
ubuntu@VM-4-14-ubuntu:~/cp$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 1565 1564 0 80 0 - 2102 do_wai pts/0 00:00:00 bash
0 T 1000 22436 1565 0 80 0 - 1378 do_sig pts/0 00:00:00 tail
0 R 1000 234428 1565 0 80 0 - 2202 - pts/0 00:00:00 ps
表头含义:
命令:
ps aux
输出:
ubuntu@VM-4-14-ubuntu:~/cp$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.5 101860 11352 ? Ss Jun30 0:01 /sbin/init
root 2 0.0 0.0 0 0 ? S Jun30 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< Jun30 0:00 [rcu_gp]
root 4 0.0 0.0 0 0 ? I< Jun30 0:00 [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< Jun30 0:00 [kworker/0:0H-kblockd]
...
表头说明:
命令:
ps -axjf
输出:
ubuntu@VM-4-14-ubuntu:~/cp$ ps -axjf
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
0 2 0 0 ? -1 S 0 0:00 [kthreadd]
2 3 0 0 ? -1 I< 0 0:00 \_ [rcu_gp]
2 4 0 0 ? -1 I< 0 0:00 \_ [rcu_par_gp]
2 6 0 0 ? -1 I< 0 0:00 \_ [kworker/0:0H-kblockd]
...
2 204925 0 0 ? -1 I 0 0:00 \_ [kworker/0:1-events]
2 213246 0 0 ? -1 I 0 0:00 \_ [kworker/0:2-events]
2 222303 0 0 ? -1 I 0 0:02 \_ [kworker/1:0-events]
2 233237 0 0 ? -1 I 0 0:00 \_ [kworker/u4:0-events_unbound]
2 234072 0 0 ? -1 I 0 0:00 \_ [kworker/u4:2-events_power_efficient]
1 805 805 805 ? -1 Ss 0 0:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startup
805 1417 1417 1417 ? -1 Ss 0 0:00 \_ sshd: ubuntu [priv]
1417 1564 1417 1417 ? -1 S 1000 0:00 | \_ sshd: ubuntu@pts/0
1564 1565 1565 1565 pts/0 236166 Ss 1000 0:00 | \_ -bash
1565 22436 22436 1565 pts/0 236166 T 1000 0:00 | \_ tail -f test2.txt
1565 236166 236166 1565 pts/0 236166 R+ 1000 0:00 | \_ ps -axjf
805 4057 4057 4057 ? -1 Ss 0 0:00 \_ sshd: ubuntu [priv]
4057 4187 4057 4057 ? -1 S 1000 0:00 \_ sshd: ubuntu@pts/1
4187 4193 4193 4193 pts/1 4193 Ss+ 1000 0:00 \_ -bash
1 1163 1162 1162 ? -1 S 0 0:01 barad_agent
1163 1164 1162 1162 ? -1 S 0 1:00 \_ barad_agent
1163 1165 1162 1162 ? -1 Sl 0 5:01 \_ barad_agent
1 1186 904 904 ? -1 SLl 0 0:22 /usr/local/qcloud/YunJing/YDLive/YDLive
1186 1214 1214 904 ? -1 SLl 0 14:25 \_ /usr/local/qcloud/YunJing/YDEyes/YDService
1214 1403 1214 904 ? -1 SLl 0 0:15 \_ /bin/sh -c sleep 100
1 1470 1470 1470 ? -1 Ss 1000 0:00 /lib/systemd/systemd --user
1470 1471 1470 1470 ? -1 S 1000 0:00 \_ (sd-pam)
...
ubuntu@VM-4-14-ubuntu:~/cp$ ps aux | grep '(cron|syslog)'
ubuntu 236578 0.0 0.0 6432 656 pts/0 S+ 16:39 0:00 grep --color=auto (cron|syslog)
ubuntu@VM-4-14-ubuntu:~/cp$ ps aux | egrep '(cron|syslog)'
message+ 743 0.0 0.2 7620 4660 ? Ss Jun30 0:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
syslog 769 0.0 0.2 224344 5084 ? Ssl Jun30 0:00 /usr/sbin/rsyslogd -n -iNONE
root 870 0.0 0.1 6812 3052 ? Ss Jun30 0:00 /usr/sbin/cron -f
ubuntu 236599 0.0 0.0 6432 720 pts/0 S+ 16:39 0:00 grep -E --color=auto (cron|syslog)
命令:
ps -aux |more
命令:
ps -aux > aux.txt
命令:
ps -o pid,ppid,pgrp,session,tpgid,comm
输出:
ubuntu@VM-4-14-ubuntu:~/cp$ ps -o pid,ppid,pgrp
PID PPID PGRP
1565 1564 1565
22436 1565 22436
237474 1565 237474
备注:ps -o pid,ppid,pgrp,session,tpgid,comm