日期:2014-05-16  浏览次数:20780 次

我使用过的Linux命令之pstree - 以树状图显示进程间的关系

我使用过的Linux命令之pstree - 以树状图显示进程间的关系

本文链接:http://codingstandards.iteye.com/blog/842156 ? (转载请注明出处)

用途说明

pstree命令以树状图显示进程间的关系(display a tree of processes)。ps命令可以显示当前正在运行的那些进程的信息,但是对于它们之间的关系却显示得不够清晰。在Linux系统中,系统调用fork可以创建子进程,通过子shell也可以创建子进程,Linux系统中进程之间的关系天生就是一棵树,树的根就是进程PID为1的init进程。

?

常用参数

格式:pstree

以树状图显示进程,只显示进程的名字,且相同进程合并显示。

?

格式:pstree -p

以树状图显示进程,还显示进程PID。

?

格式:pstree <pid>

格式:pstree -p <pid>

以树状图显示进程PID为<pid>的进程以及子孙进程,如果有-p参数则同时显示每个进程的PID。

?

格式:pstree -a

以树状图显示进程,相同名称的进程不合并显示,并且会显示命令行参数,如果有-p参数则同时显示每个进程的PID。

?

因为pstree输出的信息可能比较多,所以最好与more/less配合使用。

?

使用示例

示例一

[root@web ~]# pstree
init-+-acpid
???? |-atd
???? |-auditd-+-audispd---{audispd}
???? |??????? `-{auditd}
???? |-automount---4*[{automount}]
???? |-avahi-daemon---avahi-daemon
???? |-crond---5*[crond-+-mj.sh]
???? |????????????????? `-sendmail]
???? |-cupsd
???? |-dbus-daemon---{dbus-daemon}
???? |-events/0
???? |-events/1
???? |-gam_server
???? |-gpm
???? |-hald---hald-runner-+-hald-addon-acpi
???? |??????????????????? |-hald-addon-keyb
???? |??????????????????? `-hald-addon-stor
???? |-hcid
???? |-hidd
???? |-hpiod
???? |-java-+-java---17*[{java}]
???? |????? `-14*[{java}]
???? |-java-+-java---29*[{java}]
???? |????? `-14*[{java}]
???? |-java-+-java---34*[{java}]
???? |????? `-14*[{java}]
???? |-java---20*[{java}]
???? |-java---292*[{java}]
???? |-khelper
???? |-klogd
???? |-krfcommd
???? |-ksoftirqd/0
???? |-ksoftirqd/1
???? |-kthread-+-aio/0
???? |???????? |-aio/1
???? |???????? |-ata/0
???? |???????? |-ata/1
???? |???????? |-ata_aux
???? |???????? |-cqueue/0
???? |???????? |-cqueue/1
???? |???????? |-hd-audio0
???? |???????? |-kacpid
???? |???????? |-kauditd
???? |???????? |-kblockd/0
???? |???????? |-kblockd/1
???? |???????? |-khubd
???? |???????? |-khungtaskd
???? |???????? |-2*[kjournald]
???? |???????? |-kmpath_handlerd
???? |???????? |-kmpathd/0
???? |???????? |-kmpathd/1
???? |???????? |-kondemand/0
???? |???????? |-kondemand/1
???? |???????? |-kpsmoused
???? |???????? |-kseriod
???? |???????? |-ksnapd
???? |???????? |-kstriped
???? |???????? |-kswapd0
???? |???????? |-2*[pdflush]
???? |???????? |-rpciod/0
???? |???????? |-rpciod/1
???? |???????? |-scsi_eh_0
???? |???????? |-scsi_eh_1
???? |???????? |-scsi_eh_2
???? |???????? |-scsi_eh_3
???? |???????? |-scsi_eh_4
???? |???????? `-scsi_eh_5
???? |-loop0
???? |-mcstransd
???? |-migration/0
???? |-migration/1
???? |-6*[mingetty]
???? |-mj.sh---make---java---11*[{java}]
???? |-ntpd
???? |-pcscd---{pcscd}
???? |-portmap
???? |-python
???? |-restorecond
???? |-rpc.idmapd
???? |-rpc.statd
???? |-screen---bash---update.sh---cvs
???? |-sendmail---2*[sendmail]
???? |-sendmail
???? |-setroubleshootd---2*[{setroubleshootd}]
???? |-smartd
???? |-sshd-+-sshd---bash---update_and_rest---cvs
???? |????? |-sshd---bash---pstree
???? |????? `-sshd---bash
???? |-start_derby.sh---java---45*[{java}]
???? |-surf---8*[{surf}]
???? |-syslogd
???? |-tomcat---sleep
???? |-udevd
???? |-watchdog/0
???? |-watchdog/1
???? |-xfs
???? |-xinetd
???? `-yum-updatesd
[root@web ~]#

?

示例二

[root@web ~]# pstree -p | less
init(1)-+-acpid(2569)
??????? |-atd(2983)
??????? |-auditd(2225)-+-audispd(2227)---{audispd}(2232)
??????? |????????????? `-{auditd}(2226)
??????? |-automount(2654)-+-{automount}(2655)
??