pstree 查看进程树 命令学习

使用Centos7.6 系统

  1. 使用yum安装

使用 yum -y install pstree 下载时会报错,因为 pstree 命令的包名不是这个,使用 yum provides pstree 可以查看pstree属于哪个包,然后安装它。

Loaded plugins: fastestmirror

Repository epel is listed more than once in the configuration

Repository epel-debuginfo is listed more than once in the configuration

Repository epel-source is listed more than once in the configuration

Loading mirror speeds from cached hostfile

复制代码
[root@192 ~]# yum -y install psmisc		# 我这里已经下载过了
Loaded plugins: fastestmirror
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Package psmisc-22.20-17.el7.x86_64 already installed and latest version
Nothing to do
  1. pstree 参数介绍

    [root@192 ~]# pstree --help
    -a, --arguments 显示每个进程的命令行参数。
    -A, --ascii 使用 ASCII 字符绘制进程树,而不是默认的 UTF-8 或 VT100 字符。s
    -c, --compact 不合并具有相同命令行的进程,即使它们有相同的可执行文件也会分别显示
    -h, --highlight-all 高亮显示当前进程及其所有祖先进程。
    -H PID,
    --highlight-pid=PID 高亮显示指定 PID 的进程及其所有祖先进程。
    -g, --show-pgids 显示进程组 ID,并隐含使用 -c 选项。
    -G, --vt100 使用 VT100 字符绘制进程树。
    -l, --long 不截断长行,显示完整的命令行。
    -n, --numeric-sort 按进程 ID 对进程树进行排序。
    -N type,
    --ns-sort=type 按命名空间类型(ipc, mnt, net, pid, user, uts)排序。
    -p, --show-pids 显示进程 ID,并隐含使用 -c 选项。
    -s, --show-parents 显示选定进程的父进程。
    -S, --ns-changes 显示命名空间的变化。
    -u, --uid-changes 显示用户 ID 的变化。
    -U, --unicode 使用 UTF-8 字符绘制进程树。
    -V, --version 显示 pstree 的版本信息
    -Z,
    --security-context 显示 SELinux 安全上下文。
    PID 从指定的 PID 开始显示进程树,默认是 1(init)。
    USER 仅显示特定用户根进程的进程树。

  2. 常用参数使用效果

    3.1. 查看PID 1的进程树

    PID 1 是systemd进程,pstree 不加进程id默认是PID 1

    复制代码
    [root@192 ~]# pstree
    systemd─┬─NetworkManager─┬─dhclient
            │                └─2*[{NetworkManager}]
            ├─VGAuthService
            ├─agetty
            ├─auditd───{auditd}
            ├─crond
            ├─dbus-daemon───{dbus-daemon}
            ├─firewalld───{firewalld}
            ├─polkitd───6*[{polkitd}]
            ├─rsyslogd───2*[{rsyslogd}]
            ├─sshd─┬─sshd─┬─bash───pstree
            │      │      └─bash───sleep
            │      ├─2*[sshd───sftp-server]
            │      └─sshd───bash
            ├─systemd-journal
            ├─systemd-logind
            ├─systemd-udevd
            ├─tuned───4*[{tuned}]
            └─vmtoolsd───2*[{vmtoolsd}]

    3.2. 查看PID 1 所有子进程的ID号

    复制代码
    [root@192 ~]# pstree -p
    systemd(1)─┬─NetworkManager(586)─┬─dhclient(24421)
               │                     ├─{NetworkManager}(592)
               │                     └─{NetworkManager}(597)
               ├─VGAuthService(517)
               ├─agetty(543)
               ├─auditd(494)───{auditd}(495)
               ├─crond(536)
               ├─dbus-daemon(522)───{dbus-daemon}(532)
               ├─firewalld(550)───{firewalld}(709)
               ├─polkitd(521)─┬─{polkitd}(533)
               │              ├─{polkitd}(538)
               │              ├─{polkitd}(539)
               │              ├─{polkitd}(542)
               │              ├─{polkitd}(544)
               │              └─{polkitd}(548)
               ├─rsyslogd(907)─┬─{rsyslogd}(912)
               │               └─{rsyslogd}(914)
               ├─sshd(908)─┬─sshd(1083)─┬─bash(1098)───pstree(30025)
               │           │            └─bash(15610)───sleep(30024)
               │           ├─sshd(1087)───sftp-server(1091)
               │           ├─sshd(11200)───bash(11226)
               │           └─sshd(11207)───sftp-server(11219)
               ├─systemd-journal(359)
               ├─systemd-logind(534)
               ├─systemd-udevd(390)
               ├─tuned(904)─┬─{tuned}(1072)
               │            ├─{tuned}(1073)
               │            ├─{tuned}(1075)
               │            └─{tuned}(1076)
               └─vmtoolsd(518)─┬─{vmtoolsd}(545)
                               └─{vmtoolsd}(552)

    3.3. 仅显示父进程

    复制代码
    [root@192 ~]# pstree -s
    systemd─┬─NetworkManager─┬─dhclient
            │                └─2*[{NetworkManager}]
            ├─VGAuthService
            ├─agetty
            ├─auditd───{auditd}
            ├─crond
            ├─dbus-daemon───{dbus-daemon}
            ├─firewalld───{firewalld}
            ├─polkitd───6*[{polkitd}]
            ├─rsyslogd───2*[{rsyslogd}]
            ├─sshd─┬─sshd─┬─bash───pstree
            │      │      └─bash───sleep
            │      ├─2*[sshd───sftp-server]
            │      └─sshd───bash
            ├─systemd-journal
            ├─systemd-logind
            ├─systemd-udevd
            ├─tuned───4*[{tuned}]
            └─vmtoolsd───2*[{vmtoolsd}]

    3.4. 显示进程以及子进程的命令行参数

    复制代码
    [root@192 ~]# pstree -a 908
    sshd -D
      ├─sshd
      │   ├─bash
      │   │   └─pstree -a 908
      │   └─bash -c...
      │       └─sleep 1
      ├─sshd
      │   └─sftp-server
      ├─sshd
      │   └─bash
      └─sshd
          └─sftp-server

    3.5. 显示指定用户的进程树

    复制代码
    [root@192 opt]# pstree -u test
    bash───vmstat
相关推荐
FoldWinCard43 分钟前
D5 Linux 网络及端口命令
linux·运维·服务器
零涂毕业设计2 小时前
毕业设计模块开发-OLED显示屏(IIC协议0.96寸)STM32 ESP32 FPGA Linux驱动免费分享
linux·stm32·单片机·嵌入式·esp32·fpga·oled
tedcloud1232 小时前
OmniRoute怎么部署?开源AI模型路由平台Linux部署教程
linux·服务器·人工智能·开源·音视频
霸道流氓气质2 小时前
KMS 密钥管理服务(Key Management Service)原理与实践
linux·服务器·数据库
SakitamaX2 小时前
LVS(Linux Virtual Server)概念详解
linux·运维·lvs
小张同学a.3 小时前
Linux系统管理
linux·运维
阿龙的工作记录3 小时前
深入理解Linux设备驱动模型
linux·嵌入式硬件·工作记录
代码村新手3 小时前
Linux的基本指令
linux·运维·服务器
AAA@峥5 小时前
CentOS7 源码编译安装 MySQL5.7|SQL 基础操作 + 备份恢复完整实战
运维·数据库·sql·centos
撩得Android一次心动5 小时前
Linux编程笔记3【个人用】
linux·笔记