如何重定向一个已经运行的进程的输出 [英] How to redirect output of an already running process
问题描述
通常我会这样开始
longcommand &;
我知道你可以做这样的事情。
I know you can redirect it by doing something like
longcommand > /dev/null;
例如摆脱输出或
longcommand 2>&1 > output.log
捕捉输出。
但我有时会忘记,并想知道是否有捕获或事后重定向的一种方式。
But I sometimes forget, and was wondering if there is a way to capture or redirect after the fact.
longcommand
ctrl-z
bg 2>&1 > /dev/null
或类似的东西,这样我就可以继续使用终端没有消息在终端上弹出。
or something like that so I can continue using the terminal without messages popping up on the terminal.
推荐答案
请参阅的从正在运行的进程重定向输出
See Redirecting Output from a Running Process.
首先,我运行命令猫> foo1
在一个会话并测试从标准输入的数据被复制到文件中。然后在另一个会议上,我重定向输出。
Firstly I run the command
cat > foo1
in one session and test that data from stdin is copied to the file. Then in another session I redirect the output.
首先查找进程的PID:
Firstly find the PID of the process:
$ ps aux | grep cat
rjc 6760 0.0 0.0 1580 376 pts/5 S+ 15:31 0:00 cat
现在检查该文件处理它有开放的:
Now check the file handles it has open:
$ ls -l /proc/6760/fd
total 3
lrwx—— 1 rjc rjc 64 Feb 27 15:32 0 -> /dev/pts/5
l-wx—— 1 rjc rjc 64 Feb 27 15:32 1 -> /tmp/foo1
lrwx—— 1 rjc rjc 64 Feb 27 15:32 2 -> /dev/pts/5
现在运行GDB:
$ gdb -p 6760 /bin/cat
GNU gdb 6.4.90-debian
[license stuff snipped]
Attaching to program: /bin/cat, process 6760
[snip other stuff that's not interesting now]
(gdb) p close(1)
$1 = 0
(gdb) p creat("/tmp/foo3″, 0600)
$2 = 1
(gdb) q
The program is running. Quit anyway (and detach it)? (y or n) y
Detaching from program: /bin/cat, process 6760
在GDB的 P
命令将打印一个前pression,价值的前pression可以调用一个函数,它可以是一个系统调用...所以,我执行的close()
系统调用,并通过文件句柄1,那么我执行科瑞()
系统调用来打开一个新的文件。在科瑞()
的结果是1,这意味着它取代了previous文件句柄。如果我想使用相同的文件输出和错误,或者如果我想更换一些其他数字文件句柄然后我会需要调用 dup2()
系统调用取得这一结果。
The p
command in GDB will print the value of an expression, an expression can be a function to call, it can be a system call… So I execute a close()
system call and pass file handle 1, then I execute a creat()
system call to open a new file. The result of the creat()
was 1 which means that it replaced the previous file handle. If I wanted to use the same file for stdout and stderr or if I wanted to replace a file handle with some other number then I would need to call the dup2()
system call to achieve that result.
在这个例子中我选择使用科瑞()
而不是的open()
因为有较少的参数。为标志的C宏是不可用的,从GDB(它不使用C头文件),所以我将不得不读取头文件发现这一点 - 它并不难做到,但是这样会花费更多的时间。需要注意的是0600是为业主有读/写访问和组和其他无访问权限的八进制。它也将努力对以后的文件,该参数和运行chmod使用0。
For this example I chose to use creat()
instead of open()
because there are fewer parameter. The C macros for the flags are not usable from GDB (it doesn’t use C headers) so I would have to read header files to discover this – it’s not that hard to do so but would take more time. Note that 0600 is the octal permission for the owner having read/write access and the group and others having no access. It would also work to use 0 for that parameter and run chmod on the file later on.
这之后,我对结果进行验证:
After that I verify the result:
ls -l /proc/6760/fd/
total 3
lrwx—— 1 rjc rjc 64 2008-02-27 15:32 0 -> /dev/pts/5
l-wx—— 1 rjc rjc 64 2008-02-27 15:32 1 -> /tmp/foo3 <====
lrwx—— 1 rjc rjc 64 2008-02-27 15:32 2 -> /dev/pts/5
键入更多的数据,以猫
结果的文件中的/ tmp / foo3
追加到
如果您想要关闭您需要关闭所有文件句柄它,打开一个新的设备,可以控制tty,然后调用原始会话 setsid()
。
If you want to close the original session you need to close all file handles for it, open a new device that can be the controlling tty, and then call setsid()
.
这篇关于如何重定向一个已经运行的进程的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!