首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从写入过程外部读取打开的文件描述符

如何从写入过程外部读取打开的文件描述符
EN

Unix & Linux用户
提问于 2017-05-24 19:52:55
回答 2查看 1.8K关注 0票数 1

如何打开文件描述符并将其回显到从进程写入的终端?

我有一个备份程序,即双重性,它将其日志写入由--log-fd=16参数指定的文件描述符。

当然,如果我运行lsof -p <duplicity PID>,我会看到:

代码语言:javascript
复制
python2 9224 myuser    0r      CHR                1,3      0t0         6 /dev/null
python2 9224 myuser    1w      CHR                1,3      0t0         6 /dev/null
python2 9224 myuser    2w      CHR                1,3      0t0         6 /dev/null
python2 9224 myuser    3u  a_inode               0,11        0      7005 [eventfd]
python2 9224 myuser    4u     unix 0x0000000000000000      0t0    158199 type=STREAM
python2 9224 myuser    5u  a_inode               0,11        0      7005 [eventfd]
python2 9224 myuser    6u  a_inode               0,11        0      7005 [eventfd]
python2 9224 myuser    7r      DIR                8,3     4096  22414346 <some random file being accessed during the backup>
python2 9224 myuser    8r      CHR                1,9      0t0        11 /dev/urandom
python2 9224 myuser   15r     FIFO               0,10      0t0    157054 pipe
python2 9224 myuser   16w     FIFO               0,10      0t0    157054 pipe

但是,如果我试图在Python中打开文件描述符,就会得到一个错误:

代码语言:javascript
复制
>>> import os
>>> os.fdopen(16)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor

为什么会这样呢?如何读取文件描述符?

EN

回答 2

Unix & Linux用户

发布于 2017-05-24 21:29:33

我相信--log=fd选项是针对复杂的管道的,在这里您希望将stderrstdout从日志中分离出来。

这个对这个问题的回答给出了一个例子。下面是一个简单的例子:

代码语言:javascript
复制
#!/bin/sh
# Generate output on three different fds
echo hello >&3
echo world >&2
echo today >&1

当像这样被处决时,

代码语言:javascript
复制
./foo 2> 2.log 3> 3.log 1> 1.log

结果在

代码语言:javascript
复制
$ cat 1.log 2.log 3.log
today
world
hello
票数 1
EN

Unix & Linux用户

发布于 2020-11-06 16:43:06

使用strace (跟踪系统调用和信号)。

用法:

代码语言:javascript
复制
sudo strace -p <PID of writing process> -s 9999 -e write=<corresponding FD>

从手册页:

代码语言:javascript
复制
       -p pid      Attach to the process with the process ID pid and begin tracing.  The trace may be terminated
                   at any time by a keyboard interrupt signal (CTRL-C).  strace will respond by detaching itself
                   from  the  traced process(es) leaving it (them) to continue running.  Multiple -p options can
                   be used to attach to many processes in addition to command (which is optional if at least one
                   -p option is given).  -p "`pidof PROG`" syntax is supported.
    
       -s strsize  Specify the maximum string size to print (the default is 32).  Note that  filenames  are  not
                   considered strings and are always printed in full.
    
       -e read=set
              Perform a full hexadecimal and ASCII dump of all the data read from file descriptors listed in the
              specified set.  For example,  to  see  all  input  activity  on  file  descriptors  3  and  5  use
              -e read=3,5.   Note  that  this  is independent from the normal tracing of the read(2) system call
              which is controlled by the option -e trace=read.

       -e write=set
              Perform a full hexadecimal and ASCII dump of all the data written to file  descriptors  listed  in
              the  specified  set.   For  example,  to  see  all output activity on file descriptors 3 and 5 use
              -e write=3,5.  Note that this is independent from the normal tracing of the write(2)  system  call
              which is controlled by the option -e trace=write.

参考文献:https://man7.org/linux/man-pages/man1/strace.1.html

票数 0
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/367049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档