Podman 教程

Podman 查看容器内运行的进程

Podman 除了监听容器的运行指标外,还可以通过 podman top 命令查看容器内运行的进程,类似在主机上使用 ps 命令,支持查看进程 ID、用户、CPU 占用等信息。

基本用法

基本用法如下:

podman top 容器名或容器ID [选项]

点击查看 podman top 命令手册

  

常用使用场景示例

默认输出

显示容器内进程的基本信息(PID、USER、COMMAND 等),例如:

# 查看 my-nginx 容器内进程基本信息
C:\Users\hxstri> podman top my-nginx
USER        PID         PPID        %CPU        ELAPSED             TTY         TIME        COMMAND
root        1           0           0.000       1h42m32.467862438s  pts/0       0s          nginx: master process nginx -g daemon off;
nginx       24          1           0.000       1h42m32.468635738s  pts/0       0s          nginx: worker process
nginx       25          1           0.000       1h42m32.468696338s  pts/0       0s          nginx: worker process
nginx       26          1           0.000       1h42m32.468749938s  pts/0       0s          nginx: worker process
nginx       27          1           0.000       1h42m32.468803038s  pts/0       0s          nginx: worker process
nginx       28          1           0.000       1h42m32.468857138s  pts/0       0s          nginx: worker process
nginx       29          1           0.000       1h42m32.468904838s  pts/0       0s          nginx: worker process
nginx       30          1           0.000       1h42m32.468953138s  pts/0       0s          nginx: worker process
nginx       31          1           0.000       1h42m32.469043638s  pts/0       0s          nginx: worker process

输出字段说明:

  • USER    运行该进程的用户(容器内的用户,非主机用户)

  • PID    进程在容器内的唯一标识符(容器内 PID,与主机 PID 不冲突)

  • PPID    父进程 ID(容器内启动当前进程的父进程标识符)

  • %CPU    该进程占用的 CPU 使用率百分比

  • ELAPSED    进程从启动到当前的累计运行时间(格式通常为 时:分:秒 或 分:秒)

  • TTY    进程关联的终端(通常显示 ?,表示无终端关联,容器内后台进程常见)

  • TIME    进程累计占用的 CPU 时间(仅统计实际使用 CPU 的时长,非运行时长)

  • COMMAND    启动进程的完整命令(包括命令路径和参数,如 nginx: master process)

自定义显示字段

通过 ps 的-eo参数指定字段(如 pid,user,%cpu,command),例如:

# 显示进程 ID、用户、CPU 使用率和命令
[user@localhost ~]$ podman top my-nginx -eo pid,user,%cpu,command
    PID USER     %CPU COMMAND
      1 root      0.0 nginx: master process nginx -g daemon off;
     24 101       0.0 nginx: worker process
     25 101       0.0 nginx: worker process
     26 101       0.0 nginx: worker process
     27 101       0.0 nginx: worker process
     28 101       0.0 nginx: worker process
     29 101       0.0 nginx: worker process
     30 101       0.0 nginx: worker process
     31 101       0.0 nginx: worker process

上述命令中的 -eo 类似 ps -eo。ps -eo 是 ps 命令中用于自定义输出进程信息的常用选项组合,其中:

  • -e(等同于 -A):显示系统中所有进程(包括所有用户的进程,而非仅当前用户)。

  • -o:指定需要输出的字段列表,允许自定义显示哪些进程信息(如 PID、用户、CPU 使用率等)。

查看所有进程详情

在 podman top 最后面添加 aux 参数,类似 ps -aux 的详细输出,例如:

C:\Users\hxstri> podman top my-nginx aux
USER        PID         %CPU        %MEM        VSZ         RSS         TTY         STAT        START       TIME        COMMAND
root        1           0.0         0.0         14764       8448        ?           Ss+         15:52       0:00        nginx: master process nginx -g daemon off;
101         24          0.0         0.0         15220       3464        ?           S+          15:52       0:00        nginx: worker process
101         25          0.0         0.0         15220       3464        ?           S+          15:52       0:00        nginx: worker process
101         26          0.0         0.0         15220       3464        ?           S+          15:52       0:00        nginx: worker process
101         27          0.0         0.0         15220       3464        ?           S+          15:52       0:00        nginx: worker process
101         28          0.0         0.0         15220       3464        ?           S+          15:52       0:00        nginx: worker process
101         29          0.0         0.0         15220       3464        ?           S+          15:52       0:00        nginx: worker process
101         30          0.0         0.0         15220       3464        ?           S+          15:52       0:00        nginx: worker process
101         31          0.0         0.0         15220       3464        ?           S+          15:52       0:00        nginx: worker process

结合筛选条件

在 Linux 中, 可以配合 grep 命令查找容器内特定进程的信息,例如:

[user@localhost ~]$ podman top my-nginx | grep "nginx"
root        1           0           0.000       1h45m13.828058747s  pts/0       0s          nginx: master process nginx -g daemon off;
nginx       24          1           0.000       1h45m13.828453047s  pts/0       0s          nginx: worker process
nginx       25          1           0.000       1h45m13.828515347s  pts/0       0s          nginx: worker process
nginx       26          1           0.000       1h45m13.828580047s  pts/0       0s          nginx: worker process
nginx       27          1           0.000       1h45m13.828633947s  pts/0       0s          nginx: worker process
nginx       28          1           0.000       1h45m13.828691247s  pts/0       0s          nginx: worker process
nginx       29          1           0.000       1h45m13.828744247s  pts/0       0s          nginx: worker process
nginx       30          1           0.000       1h45m13.828796447s  pts/0       0s          nginx: worker process
nginx       31          1           0.000       1h45m13.828860347s  pts/0       0s          nginx: worker process

windows 中可以配合 findstr 命令,例如:

C:\Users\hxstri> podman top my-nginx | findstr "nginx"
root        1           0           0.000       2h2m49.775523915s  pts/0       0s          nginx: master process nginx -g daemon off;
nginx       24          1           0.000       2h2m49.776074715s  pts/0       0s          nginx: worker process
nginx       25          1           0.000       2h2m49.776181815s  pts/0       0s          nginx: worker process
nginx       26          1           0.000       2h2m49.776270815s  pts/0       0s          nginx: worker process
nginx       27          1           0.000       2h2m49.776368915s  pts/0       0s          nginx: worker process
nginx       28          1           0.000       2h2m49.776456915s  pts/0       0s          nginx: worker process
nginx       29          1           0.000       2h2m49.776543315s  pts/0       0s          nginx: worker process
nginx       30          1           0.000       2h2m49.776629015s  pts/0       0s          nginx: worker process
nginx       31          1           0.000       2h2m49.776720315s  pts/0       0s          nginx: worker process

  

说说我的看法
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
其他应用
公众号