top 的交互界面不能从管道获取输入吗

2022-06-22 16:23:19 +08:00
 wyc9296

想要看所有的包含某个字符的进程的 top 结果:

top -c -p $(ps aux|grep -i some_string|perl -ae 'print "$F[1],"'|perl -ne 's/,$//; print') # 可以运行 OK

ps aux|grep -i some_string|perl -ae 'print "$F[1],"'|perl -ne 's/,$//; print'|xargs -I {} top -c -p {} # 报错 top: failed tty get

而 top 的非交互界面却可以接收管道输入,并正常得到结果:

ps aux|grep -i some_string|perl -ae 'print "$F[1],"'|perl -ne 's/,$//; print'|xargs -I {} top -b -n 1 -c -p {} # 可以运行 OK

请各位彦祖帮忙看下为什么。谢谢

1475 次点击
所在节点    Linux
3 条回复
chengxiao
2022-06-22 16:48:31 +08:00
装 htop
wyc9296
2022-06-22 16:56:32 +08:00
@chengxiao 我的第一行命令是可以在成功运行交互界面的,可以达到我的目的。只是想知道,是不是所有交互界面都不能从管道获取输入吗
hsfzxjy
2022-06-22 17:46:45 +08:00
交互式命令通常要求 stdout 是 tty ,但管道会让 stdout 失去 tty 这一属性。你需要的是 process substitution

xargs -I {} -a <(ps aux | grep -i some_string | perl -ae 'print"$F[1],"' | perl -ne 's/,$//; print') top -c -p {}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/861413

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX