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

时间:2021-07-11 20:57:56

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?

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

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?

这不是那么干净(显示如下行:写(#,)),但有效!但它是否说正确处理UTF8?

To redirect the output, do something like

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

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

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

但目前尚不清楚如何找到合适的设备..

3 个解决方案

#1


6  

Solved in Linux (apparently Linux-specific):

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

reptyr -s PID 

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

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

#2


3  

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

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

  1. use gdb to attach to the already running process
  2. 使用gdb附加到已经运行的进程
  3. run p close(<fd>) where <fd> is the file descriptor you want to change
  4. run p close( )其中 是您要更改的文件描述符
  5. run p creat("<path to file">, <perms>) to send the output of the closed fd somewhere else
  6. 运行p creat(“ )将封闭的fd的输出发送到其他地方

See This Link for more detailed information

有关更多详细信息,请参阅此链接

#3


1  

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).

便携式Posix-ly方式无法实现!也许打开/ proc / 1234 / fd / 0和/ proc / 1234 / fd / 1和/ proc / 1234 / fd / 3伪文件(对于进程1234)可能是一个丑陋的可能性!甚至在某些情况下(例如管道)也可能不起作用。

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

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

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

并且我不相信你能够保持伪tty质量,例如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.

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

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

另请参阅屏幕命令(以及它是如何实现的!)

#1


6  

Solved in Linux (apparently Linux-specific):

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

reptyr -s PID 

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

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

#2


3  

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

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

  1. use gdb to attach to the already running process
  2. 使用gdb附加到已经运行的进程
  3. run p close(<fd>) where <fd> is the file descriptor you want to change
  4. run p close( )其中 是您要更改的文件描述符
  5. run p creat("<path to file">, <perms>) to send the output of the closed fd somewhere else
  6. 运行p creat(“ )将封闭的fd的输出发送到其他地方

See This Link for more detailed information

有关更多详细信息,请参阅此链接

#3


1  

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).

便携式Posix-ly方式无法实现!也许打开/ proc / 1234 / fd / 0和/ proc / 1234 / fd / 1和/ proc / 1234 / fd / 3伪文件(对于进程1234)可能是一个丑陋的可能性!甚至在某些情况下(例如管道)也可能不起作用。

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

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

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

并且我不相信你能够保持伪tty质量,例如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.

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

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

另请参阅屏幕命令(以及它是如何实现的!)