我正在尝试top
使用-p
选项 和来运行多个 PID xargs
。但是,top
无法运行并出现错误top: failed tty get
:
$ pgrep gvfs | paste -s -d ',' | xargs -t top -p
top -p 1598,1605,1623,1629,1635,1639,1645,1932,2744
top: failed tty get
Run Code Online (Sandbox Code Playgroud)
我使用-t
选项来xargs
查看即将执行的完整命令。看起来不错,我可以手动成功运行它:
top -p 1598,1605,1623,1629,1635,1639,1645,1932,2744
Run Code Online (Sandbox Code Playgroud)
但是,它不与 一起运行xargs
。这是为什么?
ks1*_*322 33
--open-tty
事实证明,对于xargs
交互式应用程序(例如top
. 来自man xargs:
-o, --open-tty
Reopen stdin as /dev/tty in the child process before
executing the command. This is useful if you want xargs
to run an interactive application.
Run Code Online (Sandbox Code Playgroud)
运行的命令top
应该是:
pgrep gvfs | paste -s -d ',' | xargs --open-tty top -p
Run Code Online (Sandbox Code Playgroud)
ica*_*rus 21
top
是一个交互式程序,例如您可以键入i
来切换显示空闲进程。虽然它可以安排在实践中读取,/dev/tty
但它期望 stdin 连接到终端。
对于您的示例,只需使用命令替换而不是xargs
,例如
top -p "$(pgrep gvfs | paste -s -d ',')"
Run Code Online (Sandbox Code Playgroud)
shell 首先运行 pgrep 和 Paste,获取这些命令的输出,然后使用该输出调用 top。