如何从 linux 中的文件置顶 pids 列表?

How to top a list of pids from a file in linux?

我有一个脚本来获取 pid 的列表 我想查看 cpu 使用情况,那些使用 top 的 pids 的内存使用情况。

我可以在 pid 列表的顶部使用:

top -p pid1,pid2,pid3

我有一个 returns 我需要的 pid 函数,我正在将其转换为逗号分隔列表,如下所示:

$ gal vppoc|cut -d" " -f1|xargs|sed -e 's/ /,/g'
12775,13319,14404,14549,14920,27534

但是,以下方法不起作用:

gal vppoc|cut -d" " -f1|xargs|sed -e 's/ /,/g'| top -p

它给出错误:

top: -p argument missing

需要一些帮助才能使其正常工作 - 即能够将我上面的函数返回的 pid 置顶。

谢谢

您正在使用 |(管道),它将一个进程的标准输出连接到另一个进程的标准输入。

你需要的是

top -p $(your_function_here)

例如

top -p $(gal vppoc|cut -d" " -f1|xargs|sed -e 's/ /,/g')

前面的回答是正确的。 我只给出几行以了解您的命令行中出了什么问题。 您已将 pids 提供给顶级命令 stdin 而不是作为参数。 用xargs解决很简单:

gal vppoc|cut -d" " -f1|xargs|sed -e 's/ /,/g'| xargs top -p

但是它仍然无法工作,因为顶部没有 tty。 这个可以批处理到运行 top:

gal vppoc|cut -d" " -f1|xargs|sed -e 's/ /,/g'| xargs top -b -p

它以批处理模式工作,因此您无法控制它,并且它没有连接到 tty 来刷新屏幕。

要在 tty 上工作,您不应通过管道使用 top 命令。可以 运行 命令并使用上一个答案中的方式或其他语法从内部命令获取参数值来解决这个问题:

top -p `gal vppoc|cut -d" " -f1|xargs|sed -e 's/ /,/g'`