本文链接: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)
| |-{automount}(2656)
| |-{automount}(2659)
| `-{automount}(2662)
|-avahi-daemon(3026)---avahi-daemon(3027)
|-crond(2916)-+-crond(14707)-+-mj.sh(14709)
| | `-sendmail(14773)
| |-crond(14708)-+-mj.sh(14710)
| | `-sendmail(14754)
| `-crond(26312)-+-mj.sh(26314)
| `-sendmail(26387)
|-cupsd(2717)
|-dbus-daemon(2446)---{dbus-daemon}(2447)
|-events/0(8)
|-events/1(9)
|-gam_server(3175)
|-gpm(2902)
|-hald(2587)---hald-runner(2588)-+-hald-addon-acpi(2596)
| |-hald-addon-keyb(13708)
| `-hald-addon-stor(2606)
|-hcid(2477)
|-hidd(2628)
|-hpiod(2676)
|-java(1572)-+-java(1586)-+-{java}(1590)
| | |-{java}(1591)
| | |-{java}(1592)
| | |-{java}(1593)
| | |-{java}(1594)
| | |-{java}(1595)
| | |-{java}(1596)
| | |-{java}(1597)
| | |-{java}(1598)
| | |-{java}(1599)
| | |-{java}(1600)
| | |-{java}(1782)
| | |-{java}(1783)
:
[root@web ~]# pstree -a | less
init
|-acpid
|-atd
|-auditd
| |-audispd
| | `-{audispd}
| `-{auditd}
|-automount
| |-{automount}
| |-{automount}
| |-{automount}
| `-{automount}
|-avahi-daemon
| `-avahi-daemon
|-crond
| |-crond
| | |-(mj.sh)
| | `-sendmail
| |-crond
| | |-(mj.sh)
| | `-sendmail
| |-crond
| | |-(mj.sh)
| | `-sendmail -FCronDaemon -i -odi -oem -oi -t
| |-crond
| | |-(mj.sh)
| | `-sendmail -FCronDaemon -i -odi -oem -oi -t
| `-crond
| |-(mj.sh)
| `-sendmail -FCronDaemon -i -odi -oem -oi -t
|-cupsd
|-dbus-daemon --system
| `-{dbus-daemon}
|-(events/0)
|-(events/1)
|-gam_server
|-gpm -m /dev/input/mice -t exps2
|-hald
| `-hald-runner
| |-hald-addon-acpi
:
[root@web ~]# pstree 2701
sshd-+-sshd---bash---update_and_rest---cvs
|-sshd---bash---pstree
`-sshd---bash
[root@web ~]# pstree -p 2701
sshd(2701)-+-sshd(6745)---bash(6749)---update_and_rest(14193)---cvs(14194)
|-sshd(12626)---bash(12630)---pstree(15588)
`-sshd(13324)---bash(13328)
[root@web ~]#