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
相关推荐
Sapphire~5 小时前
Linux-07 ubuntu 的 chrome 启动不了
linux·chrome·ubuntu
伤不起bb5 小时前
NoSQL 之 Redis 配置与优化
linux·运维·数据库·redis·nosql
广东数字化转型5 小时前
nginx怎么使用nginx-rtmp-module模块实现直播间功能
linux·运维·nginx
啵啵学习6 小时前
Linux 里 su 和 sudo 命令这两个有什么不一样?
linux·运维·服务器·单片机·ubuntu·centos·嵌入式
半桔6 小时前
【Linux手册】冯诺依曼体系结构
linux·缓存·职场和发展·系统架构
网硕互联的小客服7 小时前
如何利用Elastic Stack(ELK)进行安全日志分析
linux·服务器·网络·安全
冰橙子id7 小时前
linux——磁盘和文件系统管理
linux·运维·服务器
无聊的小坏坏8 小时前
环境变量深度解析:从配置到内核的全链路指南
linux·bash
才鲸嵌入式8 小时前
01 Ubuntu20.04下编译QEMU8.2.4,交叉编译32位ARM程序,运行ARM程序的方法
linux·c语言·单片机·嵌入式·arm·qemu·虚拟机
伤不起bb10 小时前
Kafka 消息队列
linux·运维·分布式·kafka