使用命令行捕获进程启动后的 STDIN / STDERR / STDOUT?

发布于 2024-12-29 16:26:34 字数 457 浏览 2 评论 0 原文

谢谢!我的用户案例:我开始了一个漫长的交互式“配置”过程(例如在“屏幕”下),然后意识到我需要始终回答“否”,直到看到特定的关键字。手动执行此操作似乎浪费时间(并不是说我很容易错过关键字..)

因此,我似乎想将 STDERR / STDOUT (的副本)通过管道传输到过滤器, 并且还能够注入(控制台)进程的 STDIN, 启动后,使用命令行?有现成的解决方案吗?

以下工具似乎有帮助。要捕获输出,请使用

strace -ewrite -p $PID

It's not that clean (显示类似以下的行: write(#,) ),但是有效! 但它说正确处理UTF8吗?

要重定向输出,请执行类似的操作

printf '..input..' >/dev/pts/33

但尚不清楚如何找到正确的设备..

Thanks! My usercase: I started a lengthy interactive 'configure' process (say under 'screen'), and then realised I need to always answer 'no' until I see a particular keyword. Seems a waste of time to do this by hand (not to say that I can easily miss the keyword..)

Thus it seems I want to pipe (a copy of) STDERR / STDOUT to a filter,
and also be able to inject into the STDIN of a (console) process,
AFTER it's been started, using command line? Is there a ready-made solution?

The following tools seem help. To capture output, use

strace -ewrite -p $PID

It's not that clean (shows lines like: write(#,) ), but works!
But does it say handle UTF8 correctly?

To redirect the output, do something like

printf '..input..' >/dev/pts/33

But it is not clear how to find the right device..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2025-01-05 16:26:34

在 Linux 中解决(显然是 Linux 特定的):

reptyr -s PID 

将进程附加到另一个终端和/或将其输入和输出公开为管道。

Solved in Linux (apparently Linux-specific):

reptyr -s PID 

attaches a process to another terminal and/or exposes its input and output as pipes.

躲猫猫 2025-01-05 16:26:34

这是可能的,但并不漂亮。流程如下:

  1. 使用 gdb 附加到已经运行的进程
  2. run p close() where 是您要更改的文件描述符
  3. run p creat(", ) 以将关闭的 fd 的输出发送到某处否则

参见此链接了解更多详细信息

This is possible but it's not pretty. The process is as follows:

  1. use gdb to attach to the already running process
  2. run p close(<fd>) where <fd> is the file descriptor you want to change
  3. run p creat("<path to file">, <perms>) to send the output of the closed fd somewhere else

See This Link for more detailed information

心碎的声音 2025-01-05 16:26:34

你为什么要这么做?

以可移植的 Posix-ly 方式是不可能的!也许open-ing /proc/1234/fd/0/proc/1234/fd/1/proc/ 1234/fd/3 伪文件(对于进程 1234)可能是一种丑陋的可能性!即使这在某些情况下也可能不起作用(例如对于管道)。

特别是,我相信如果没有人正在读取管道,发送到进程的 SIGPIPE 的语义将会被破坏......

并且我不相信您能够保留伪 tty例如 stdout 的质量。

所以基本上,你最好找到一种不同的方法来实现你没有明确解释的总体目标。

如果您的用例是一些奇怪的 configure 脚本,您可以重新启动它并使用您自己的一些脚本(在 shell、python、perl 等中)提供它。不要浪费时间尝试捕获现有的 configure 进程,只需适当地重新启动它即可。

另请查看 screen 命令(以及它是如何实现的!)

Why do you want to do that??

It is not possible in a portable Posix-ly way! Maybe open-ing the /proc/1234/fd/0 and /proc/1234/fd/1 and /proc/1234/fd/3 pseudo-files (for process 1234) could be an ugly possiblity! And even that might not work in some cases (e.g. for pipes).

In particular, I believe that the semantics of SIGPIPE sent to the process if nobody is reading a pipe would be broken...

And I don't believe that you would be able to keep the pseudo-tty quality of e.g. stdout .

So basically, you'll better find a different way to achieve your overall goals, which you did not explain explicitly.

If your use-case is some weird configure script, you could restart it and feed it with some script of your own (in shell, python, perl, etc...). Don't lose time to try to catch an existing configure process, just restart it appropriately.

Look also at the screen command (and how it is implemented!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文