如何打开文件描述符并将其回显到从进程写入的终端?
我有一个备份程序,即双重性,它将其日志写入由--log-fd=16参数指定的文件描述符。
当然,如果我运行lsof -p <duplicity PID>,我会看到:
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中打开文件描述符,就会得到一个错误:
>>> import os
>>> os.fdopen(16)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor为什么会这样呢?如何读取文件描述符?
发布于 2017-05-24 21:29:33
我相信--log=fd选项是针对复杂的管道的,在这里您希望将stderr和stdout从日志中分离出来。
这个对这个问题的回答给出了一个例子。下面是一个简单的例子:
#!/bin/sh
# Generate output on three different fds
echo hello >&3
echo world >&2
echo today >&1当像这样被处决时,
./foo 2> 2.log 3> 3.log 1> 1.log结果在
$ cat 1.log 2.log 3.log
today
world
hello发布于 2020-11-06 16:43:06
使用strace (跟踪系统调用和信号)。
用法:
sudo strace -p <PID of writing process> -s 9999 -e write=<corresponding FD>从手册页:
-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://unix.stackexchange.com/questions/367049
复制相似问题