Linux 命令 - ps、pstree
转载请注明出处:https://www.cnblogs.com/kelamoyujuzhen/p/9814883.html
ps
简介
ps(processes status)是Unix / Linux上的一个实用程序,用于查看系统上正在运行的进程的信息。ps命令默认安装,即使最小化安装也会有这个命令。ps命令从/proc文件系统中的虚拟文件中读取信息。 ps命令是系统管理的重要工具之一,专门用于进程监视,以帮助程序开发了解Linux系统上的最新情况。
请注意,ps生成带有标题行的输出,表示每列信息的含义。
用法演示
ps aux
a = show processes for all users 与terminal相关的进程
u = display the process's user/owner
x = also show you (runner of the ps command, root in this case) all processes, not just ones attached to a terminal. This will include processes such as services like crond, upowerd, etc. 除了与终端相关的进程,还显示与terminal无关的进程
由于加上x后输出信息较多,示例没有加x
[root@localhost ~]# ps au USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1414 0.0 0.0 4060 596 tty1 Ss+ 06:05 0:00 /sbin/mingetty /dev/tty1 root 1416 0.0 0.0 4060 596 tty2 Ss+ 06:05 0:00 /sbin/mingetty /dev/tty2 root 1418 0.0 0.0 4060 596 tty3 Ss+ 06:05 0:00 /sbin/mingetty /dev/tty3 root 1420 0.0 0.0 4060 592 tty4 Ss+ 06:05 0:00 /sbin/mingetty /dev/tty4 root 1422 0.0 0.0 4060 588 tty5 Ss+ 06:05 0:00 /sbin/mingetty /dev/tty5 root 1425 0.0 0.0 4060 592 tty6 Ss+ 06:05 0:00 /sbin/mingetty /dev/tty6 root 1431 0.0 0.0 108320 1916 pts/0 Ss 06:06 0:00 -bash root 1535 0.0 0.0 110248 1180 pts/0 R+ 07:11 0:00 ps au
- USER:该 process 属于哪个用户
- PID :该 process 的PID
- %CPU:该 process 使用掉的 CPU 资源百分比;
- %MEM:该 process 所占用的实体内存百分比;
- VSZ :该 process 使用掉的虚拟内存量 (Kbytes)
- RSS :该 process 占用的固定的内存量 (Kbytes)
- TTY :该 process 是在那个终端机上面运行,若与终端机无关则显示 ?,另外, tty1-tty6 是本机上面的登陆者程序,若为 pts/0 等等的,则表示为由网络连接进主机的程序。
- STAT:该程序目前的状态,状态显示与 ps -l 的 S 旗标相同 (R/S/T/Z)
- START:该 process 被触发启动的时间;
R:running
S:interruptable sleeping 可中断睡眠
D: uninterruptable sleeping不可中断睡眠
T: stopped
Z: zombie僵尸进程。通常,造成僵尸程序的成因是因为该程序应该已经运行完毕,或者是因故应该要终止了, 但是该程序的父程序却无法完整的将该程序结束掉,而造成那个程序一直存在内存当中。 如果你发现在某个程序的 CMD 后面还接上 <defunct> 时,就代表该程序是僵尸程序啦,例如:
apache 8683 0.0 0.9 83384 9992 ? Z 14:33 0:00 /usr/sbin/httpd <defunct>
通常僵尸进程交由init或者systemd进程处理,如果init或者systemd也干不死,那只能reboot了。
+: 前台进程
l: 多线程进程
N:低优先级进程
<:高优先级进程
s: session leader
- TIME :该 process 实际使用 CPU 运行的时间。
- COMMAND:该程序的实际命令为何?
ps -f 或者 -F可以多显示几列。其中-F显示的能更多些
[root@localhost ~]# ps -f UID PID PPID C STIME TTY TIME CMD root 4360 1305 0 10:11 pts/1 00:00:00 -bash root 4384 4360 0 10:11 pts/1 00:00:00 ps -f [root@localhost ~]# ps -F UID PID PPID C SZ RSS PSR STIME TTY TIME CMD root 4360 1305 0 28859 2020 0 10:11 pts/1 00:00:00 -bash root 4387 4360 0 38831 1844 1 10:11 pts/1 00:00:00 ps -F
使用-L还可以显示NLWP (number of threads) 和 LWP (thread ID) 列
[root@localhost ~]# ps -L PID LWP TTY TIME CMD 4360 4360 pts/1 00:00:00 bash 4804 4804 pts/1 00:00:00 ps [root@localhost ~]# ps -Lf UID PID PPID LWP C NLWP STIME TTY TIME CMD root 4360 1305 4360 0 1 10:11 pts/1 00:00:00 -bash root 4814 4360 4814 0 1 10:20 pts/1 00:00:00 ps -Lf [root@localhost ~]# ps -LF UID PID PPID LWP C NLWP SZ RSS PSR STIME TTY TIME CMD root 4360 1305 4360 0 1 28859 2024 0 10:11 pts/1 00:00:00 -bash root 4824 4360 4824 0 1 38831 1840 1 10:20 pts/1 00:00:00 ps -LF
显示Linux系统下所有活跃进程
ps -A 或 ps -e
[root@localhost proc]# ps -e PID TTY TIME CMD 1 ? 00:00:01 systemd 2 ? 00:00:00 kthreadd 3 ? 00:00:00 ksoftirqd/0 5 ? 00:00:00 kworker/0:0H 6 ? 00:00:00 kworker/u256:0 7 ? 00:00:00 migration/0 8 ? 00:00:00 rcu_bh 9 ? 00:00:00 rcu_sched 10 ? 00:00:00 lru-add-drain 11 ? 00:00:00 watchdog/0 12 ? 00:00:00 watchdog/1 13 ? 00:00:00 migration/1 14 ? 00:00:00 ksoftirqd/1 16 ? 00:00:00 kworker/1:0H 17 ? 00:00:00 watchdog/2 18 ? 00:00:00 migration/2 19 ? 00:00:00 ksoftirqd/2 21 ? 00:00:00 kworker/2:0H 22 ? 00:00:00 watchdog/3 23 ? 00:00:00 migration/3 24 ? 00:00:00 ksoftirqd/3 26 ? 00:00:00 kworker/3:0H 28 ? 00:00:00 kdevtmpfs 29 ? 00:00:00 netns 30 ? 00:00:00 khungtaskd 31 ? 00:00:00 writeback 32 ? 00:00:00 kintegrityd 33 ? 00:00:00 bioset 34 ? 00:00:00 kblockd 35 ? 00:00:00 md 36 ? 00:00:00 edac-poller 38 ? 00:00:00 kworker/1:1 43 ? 00:00:00 kswapd0 44 ? 00:00:00 ksmd 45 ? 00:00:00 khugepaged 46 ? 00:00:00 crypto 54 ? 00:00:00 kthrotld 56 ? 00:00:00 kmpath_rdacd 57 ? 00:00:00 kaluad 58 ? 00:00:00 kpsmoused 59 ? 00:00:01 kworker/0:2 60 ? 00:00:00 ipv6_addrconf 73 ? 00:00:00 deferwq 105 ? 00:00:00 kauditd 290 ? 00:00:00 mpt_poll_0 292 ? 00:00:00 ata_sff 293 ? 00:00:00 mpt/0 300 ? 00:00:00 scsi_eh_0 301 ? 00:00:00 scsi_tmf_0 302 ? 00:00:00 kworker/u256:2 303 ? 00:00:00 scsi_eh_1 304 ? 00:00:00 scsi_tmf_1 305 ? 00:00:00 scsi_eh_2 306 ? 00:00:00 scsi_tmf_2 309 ? 00:00:00 ttm_swap 310 ? 00:00:00 irq/16-vmwgfx 322 ? 00:00:00 kworker/3:2 330 ? 00:00:00 kworker/1:2 387 ? 00:00:00 kdmflush 388 ? 00:00:00 bioset 398 ? 00:00:00 kdmflush 400 ? 00:00:00 bioset 413 ? 00:00:00 bioset 414 ? 00:00:00 xfsalloc 415 ? 00:00:00 xfs_mru_cache 416 ? 00:00:00 xfs-buf/dm-0 417 ? 00:00:00 xfs-data/dm-0 418 ? 00:00:00 xfs-conv/dm-0 419 ? 00:00:00 xfs-cil/dm-0 420 ? 00:00:00 xfs-reclaim/dm- 421 ? 00:00:00 xfs-log/dm-0 422 ? 00:00:00 xfs-eofblocks/d 423 ? 00:00:00 xfsaild/dm-0 424 ? 00:00:00 kworker/0:1H 493 ? 00:00:00 systemd-journal 520 ? 00:00:00 lvmetad 529 ? 00:00:00 systemd-udevd 548 ? 00:00:00 nfit 613 ? 00:00:00 xfs-buf/sda1 614 ? 00:00:00 xfs-data/sda1 615 ? 00:00:00 xfs-conv/sda1 616 ? 00:00:00 xfs-cil/sda1 617 ? 00:00:00 xfs-reclaim/sda 618 ? 00:00:00 xfs-log/sda1 619 ? 00:00:00 xfs-eofblocks/s 620 ? 00:00:00 xfsaild/sda1 623 ? 00:00:00 kdmflush 624 ? 00:00:00 bioset 631 ? 00:00:00 xfs-buf/dm-2 632 ? 00:00:00 xfs-data/dm-2 633 ? 00:00:00 xfs-conv/dm-2 634 ? 00:00:00 xfs-cil/dm-2 635 ? 00:00:00 xfs-reclaim/dm- 636 ? 00:00:00 xfs-log/dm-2 637 ? 00:00:00 xfs-eofblocks/d 638 ? 00:00:00 xfsaild/dm-2 660 ? 00:00:00 auditd 683 ? 00:00:00 dbus-daemon 686 ? 00:00:00 polkitd 688 ? 00:00:00 irqbalance 689 ? 00:00:00 systemd-logind 693 ? 00:00:00 crond 698 ? 00:00:00 login 705 ? 00:00:00 firewalld 707 ? 00:00:00 NetworkManager 754 ? 00:00:00 kworker/2:1H 826 ? 00:00:00 kworker/3:1H 838 ? 00:00:00 dhclient 1021 ? 00:00:00 tuned 1022 ? 00:00:00 sshd 1023 ? 00:00:00 rsyslogd 1170 ? 00:00:00 master 1176 ? 00:00:00 pickup 1177 ? 00:00:00 qmgr 1284 tty1 00:00:00 bash 1299 ? 00:00:00 kworker/1:1H 1305 ? 00:00:00 sshd 1309 pts/0 00:00:00 bash 1455 ? 00:00:00 kworker/3:3 1477 ? 00:00:00 memcached 2198 ? 00:00:00 kworker/2:0 2321 ? 00:00:00 kworker/0:0 2894 ? 00:00:00 kworker/2:2 3163 ? 00:00:00 kworker/2:1 3331 ? 00:00:00 kworker/0:1 3346 pts/0 00:00:00 ps [root@localhost proc]# ps -A PID TTY TIME CMD 1 ? 00:00:01 systemd 2 ? 00:00:00 kthreadd 3 ? 00:00:00 ksoftirqd/0 5 ? 00:00:00 kworker/0:0H 6 ? 00:00:00 kworker/u256:0 7 ? 00:00:00 migration/0 8 ? 00:00:00 rcu_bh 9 ? 00:00:00 rcu_sched 10 ? 00:00:00 lru-add-drain 11 ? 00:00:00 watchdog/0 12 ? 00:00:00 watchdog/1 13 ? 00:00:00 migration/1 14 ? 00:00:00 ksoftirqd/1 16 ? 00:00:00 kworker/1:0H 17 ? 00:00:00 watchdog/2 18 ? 00:00:00 migration/2 19 ? 00:00:00 ksoftirqd/2 21 ? 00:00:00 kworker/2:0H 22 ? 00:00:00 watchdog/3 23 ? 00:00:00 migration/3 24 ? 00:00:00 ksoftirqd/3 26 ? 00:00:00 kworker/3:0H 28 ? 00:00:00 kdevtmpfs 29 ? 00:00:00 netns 30 ? 00:00:00 khungtaskd 31 ? 00:00:00 writeback 32 ? 00:00:00 kintegrityd 33 ? 00:00:00 bioset 34 ? 00:00:00 kblockd 35 ? 00:00:00 md 36 ?推荐阅读
添加路由的 linux 命令是什么?
Linux 工具 vim 概念、操作和基本命令说明
测试工程师必备的 Linux 命令
Linux 下有哪些重启命令?
centos 精通学习 用于 Linux 目录管理的 ls-key 命令
Linux kill 命令
Linux 学习] SSH 连接出现主机密钥验证失败的原因和解决方案以及 ssh-keygen 命令的用法
2024-04-23 用于查看内存占用情况的 linux 命令 free -h 和 cat /proc/meminfo- i. 要查看 Linux 系统中的内存占用情况,可以使用 free 命令或 top 命令。下面简要介绍这两条命令:
请谨慎使用这些 Linux 命令!
Linux JVM 相关命令和案例研究