将程序附加到进程 stdout/stderr

发布于 2024-12-11 02:57:15 字数 290 浏览 0 评论 0原文

我有一个程序,需要附加到给定 pid 的 stdout/stderr (从文件中获取)。 这应该如何完成?这可能吗?

编辑1:

我有一个启动/停止服务器程序的监视器程序。但是,监视器可以关闭/重新打开,并且应该挂钩现有服务器标准输出以读取标准输出上写入的错误(以及基于某些监视器请求的一些输出)。

编辑2:

我构建了服务器和监视器,所以我有两者的来源,问题是服务器“回答”标准输出上的某些监视器请求,我不想添加另一个进程间通信部分

I have a program where I need to attach to stdout/stderr of a given pid (fetched from a file).
How this should be done?Is this even possible?

EDIT 1:

I have a monitor program that starts/stops a server program. However, the monitor can be closed/reopened and should hook to existent server stdout to read errors that are written on stdout (and also some output based on some monitor requests).

EDIT 2:

I built server and monitor so I have sources of both, the problem is that the server "answer" to some monitor requests on the stdout, I don't want to add another interprocess comunication part

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

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

发布评论

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

评论(3

魔法唧唧 2024-12-18 02:57:15

当进程正在运行时,没有标准的 Unix 方法可以拦截另一个进程的输出并在目标进程启动后开始捕获它。

如果您自己通过 execve 启动这些进程,则只需通过 pipe(2) 设置管道并重定向其描述符(通过 dup2(2)) 到子进程的 stdin 和 stdout。这样,父级将能够通过管道写入/读取子级的标准输入/标准输出。

关于编辑后的问题:这似乎很适合 Unix fifo 文件。

fifo 文件(或命名管道)看起来像一个文件,但实际上是作为管道实现的。

因此,只需创建一个 fifo 文件(使用 mkfifo(1) 命令),通过将其 stdin 和 stdout 描述符重定向到该文件(使用 < 和shell 的 > 运算符),您可以随时从中读取内容。

While a process is running, there isn't a standard Unix way to intercept its output from another process and start capturing it after the target process has been started.

If you are starting these processes yourself, via execve, you can simply set up a pipe via pipe(2) and redirect its descriptors (via dup2(2)) to the child process' stdin and stdout. This way the parent will be able to write/read to the child's stdin/stdout through the pipe.

Regarding your question after the edit: this seems like a good fit for a Unix fifo file.

A fifo file (or a named pipe) appears like a file, but is implemented as a pipe under the bonnet.

So just create a fifo file (with the mkfifo(1) command), start the server application by redirecting its stdin and stdout descriptors to that file (with the < and > operators of the shell), and you'll be able to read from it anytime.

恍梦境° 2024-12-18 02:57:15

从未尝试过,但您可以查看 /proc/$pid/ 目录,如果可以(具有适当的权限)附加到那里的文件描述符条目。否则我无法想象这怎么可能。


编辑(在获得更多详细信息后)

您声明,您的进程将负责启动/停止该服务器进程 - 这使事情变得更加容易:)

因为这是家庭作业,我将画出图片:

  • 创建命名服务器的 stdinstdout

    的管道

  • 当启动服务器,将其stdin/stdout与命名管道连接

  • 启动时您的客户端,从命名管道读取/写入

Never tried, but you may look at the /proc/$pid/ directory if it is possible (with proper permissions) to attach to the file descriptor entries there. Otherwise I couldn't imagine how this would be possible.


EDIT (after getting more details)

You state, that your process will be respnsible to start/stop that server process - THIS makes things a lot easier :)

As this is homework, I'll just draw the picture:

  • create named pipes for ther server's stdin and stdout

  • when starting the server, connect its stdin/stdout with the named pipes

  • when starting your client, read/write from/to the named pipes

若沐 2024-12-18 02:57:15

您是否可以选择配置服务器,以便将输出发送到日志文件,或者除了 stdout 之外?在 unix 机器上,您可以通过 tee 运行服务器以将 stdout 记录到文件中:

$ server | tee server.log

然后尾部 server.log 即可轻松获取最新输出。

$ tail -f server.log

Do you have the option of configuring the server so that it sends output to a log file instead, or in addition to, stdout? On a unix box, you could run the server through tee to log stdout to a file:

$ server | tee server.log

Then it is a simple matter to tail server.log to get the latest output.

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