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
相关推荐
SunTecTec5 分钟前
Linux 在个人家目录下添加环境变量 如FLINK_PROPERTIES=“jobmanager.rpc.address: jobmanager“
linux·运维·服务器
davysiao8 分钟前
CentOS 使用国内镜像安装 nvm 和 Node.js 完整指南
linux·centos·node.js
杨凯凡11 分钟前
Linux性能大师:htop与nmon实战对比分析
linux·运维·服务器
B64A-消闲38 分钟前
case和字符串操作
linux·运维·服务器
码农研究僧1 小时前
Ubuntu 磁盘空间占用清理(宝塔)
linux·运维·ubuntu
{⌐■_■}1 小时前
【计网】认识跨域,及其在go中通过注册CORS中间件解决跨域方案,go-zero、gin
java·linux·开发语言·c++·中间件·golang·gin
若疆赤云online1 小时前
window和ubuntu自签证书
linux·运维·ubuntu
像风一样自由20201 小时前
图像识别系统 - Ubuntu部署指南(香橙派开发板测试)-学习记录1
linux·学习·ubuntu
yangshuo12811 小时前
新ubuntu物理机开启ipv6让外网访问
linux·运维·ubuntu
ZZZKKKRTSAE1 小时前
快速上手Linux的Web服务器的部署及优化
linux·运维·服务器·web