Linux 附加到进程输出以供查看

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/715751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:10:55  来源:igfitidea点击:

Attach to a processes output for viewing

linuxloggingcommand-line-interface

提问by aggitan

How would I 'attach' a console/terminal-view to an applications output so I can see what it may be saying?

我如何将控制台/终端视图“附加”到应用程序输出,以便我可以看到它可能在说什么?

How would I detach from an applications output without killing the application?

如何在不终止应用程序的情况下从应用程序输出中分离?

Normally if you fire up a talkative application using the command line you get to see all kinds of wonderful output. However, let's say I have a particularly chatty programming running, like KINO, and I want to view its output at any given moment without restarting it through the command line. I cannot; at least I don't know how.

通常,如果您使用命令行启动一个健谈的应用程序,您会看到各种精彩的输出。但是,假设我有一个特别健谈的程序正在运行,例如 KINO,我想在任何给定时刻查看其输出,而无需通过命令行重新启动它。我不能; 至少我不知道怎么做。

采纳答案by Don Werve

There are a few options here. One is to redirect the output of the command to a file, and then use 'tail' to view new lines that are added to that file in real time.

这里有几个选项。一种是将命令的输出重定向到一个文件,然后使用“tail”实时查看添加到该文件的新行。

Another option is to launch your program inside of 'screen', which is a sort-of text-based Terminal application. Screen sessions can be attached and detached, but are nominally meant only to be used by the same user, so if you want to share them between users, it's a big pain in the ass.

另一种选择是在“屏幕”内启动您的程序,这是一种基于文本的终端应用程序。屏幕会话可以附加和分离,但名义上只能由同一用户使用,所以如果你想在用户之间共享它们,这是一个很大的麻烦。

回答by yves Baumes

How would I 'attach' a console/terminal-view to an applications output so I can see what it may be saying?

我如何将控制台/终端视图“附加”到应用程序输出,以便我可以看到它可能在说什么?

About this question, I know it is possible to catch the output, even when you didn't launch sceen command before launching the processus.

关于这个问题,我知道即使在启动 processus 之前没有启动 sceen 命令,也可以捕获输出。

While I never tried it, I've found an interesting article which explains how to do using GDB (and without restarting your process).

虽然我从未尝试过,但我发现了一篇有趣的文章,它解释了如何使用 GDB(并且无需重新启动进程)。

redirecting-output-from-a-running-process

从正在运行的进程重定向输出

Basically:

基本上:

  1. Check the open files list for your process, thanks to /proc/xxx/fd
  2. Attachyour process with GDB
  3. While it is paused, close the file you are interested in, calling close()function (you can any function of your process in GDB. I suspect you need debug symbols in your process..)
  4. Open the a new file calling the create()or open() function. (Have a look in comments at the end, you'll see people suggest to use dup2()to ensure the same handle will be in use)
  5. Detachthe process and let in run.
  1. 检查您的进程的打开文件列表,感谢/proc/xxx/fd
  2. 使用 GDB附加您的流程
  3. 暂停时,关闭您感兴趣的文件,调用close()函数(您可以在 GDB 中使用您进程的任何函数。我怀疑您的进程中需要调试符号..)
  4. 打开调用create()或 open() 函数的新文件。(看看最后的评论,你会看到人们建议使用dup2()来确保使用相同的句柄)
  5. 分离进程并让其运行。

By the way, if you are running a linux OS on i386 box, comments are talking about a better tool to redirect output to a new console : 'retty'. If so, consider its use.

顺便说一句,如果您在 i386 机器上运行 linux 操作系统,评论中谈论的是将输出重定向到新控制台的更好工具:'retty'。如果是这样,请考虑使用它。

回答by Paul Scheltema

I was looking for this exact same thing and found that you can do:

我一直在寻找完全相同的东西,发现你可以这样做:

strace -ewrite -p $PID

It's not exactly what you needed, but it's quite close.

这不完全是你需要的,但它非常接近。

I tried the redirecting output, but that didn't work for me. Maybe because the process was writing to a socket, I don't know.

我尝试了重定向输出,但这对我不起作用。也许是因为该进程正在写入套接字,我不知道。

回答by licorna

I think I have a simpler solution here. Just look for a directory whose name corresponds to the PID you are looking for, under the pseudo-filesystem accessible under the /procpath. So if you have a program running, whose ID is 1199, cdinto it:

我想我在这里有一个更简单的解决方案。只需在/proc路径下可访问的伪文件系统下查找名称与您要查找的 PID 相对应的目录。因此,如果您正在运行一个 ID 为 1199 的程序cd

$ cd /proc/1199

Then look for the fddirectory underneath

然后查找fd下面的目录

$ cd fd

This fddirectory hold the file-descriptors objects that your program is using (0: stdin, 1: stdout, 2: stderr) and just tail -fthe one you need - in this case, stdout):

fd目录包含您的程序正在使用的文件描述符对象(0:stdin、1:stdout、2:stderr)以及tail -f您需要的对象 - 在本例中为 stdout):

$ tail -f 1

回答by RedScourge

I wanted to remotely watch a yum upgrade process that had been run locally, so while there were probably more efficient ways to do this, here's what I did:

我想远程观看在本地运行的 yum 升级过程,所以虽然可能有更有效的方法来做到这一点,但我是这样做的:

watch cat /dev/vcsa1

Obviously you'd want to use vcsa2, vcsa3, etc., depending on which terminal was being used.

显然,您希望使用 vcsa2、vcsa3 等,具体取决于使用的终端。

So long as my terminal window was of the same width as the terminal that the command was being run on, I could see a snapshot of their current output every two seconds. The other commands recommended elsewhere did not work particularly well for my situation, but that one did the trick.

只要我的终端窗口与运行命令的终端的宽度相同,我就可以每两秒看到一个当前输出的快照。在其他地方推荐的其他命令对我的情况并不是特别好,但是那个成功了。

回答by akki

For me, this worked:

对我来说,这有效:

  1. Login as the owner of the process (even rootis denied permission)

    ~$ su - process_owner
    
  2. Tail the file descriptor as mentioned in many other answers.

    ~$ tail -f /proc/<process-id>/fd/1 # (0: stdin, 1: stdout, 2: stderr)
    
  1. 以进程所有者身份登录(甚至root被拒绝权限)

    ~$ su - process_owner
    
  2. 尾随许多其他答案中提到的文件描述符。

    ~$ tail -f /proc/<process-id>/fd/1 # (0: stdin, 1: stdout, 2: stderr)