百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

reredirect

> 编程语言
开源

用于动态重定向正在运行进程的输出的工具

604 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

用于动态重定向正在运行进程的输出的工具

reredirect - A tool to dynamically redirect outputs of a running program

reredirect is a utility for taking an existing running program and attaching its output (standard output and error output) to files or to another process.

Using reredirect, you can log output of an already launched process, redirect debug output of a background process to /dev/null or to a pager as if you launched it with > or |.

Installation

Installation uses common rules for C projects. Compile with:

make

Then, install with:

bash
sudo make install

Usage

Simple usage is:

reredirect -m FILE PID

It will redirect output of PID to FILE. It is also possible to redirect standard output and error output in different files:

reredirect -o FILE1 -e FILE2 PID

-m option is just a shortcut to -o FILE -e FILE.

After being launched, reredirect give you the ability to restore state of PID. It will look something like this :

reredirect -N -O 5 -E 3 5453

-O and -E act as -o and -e but with already opened file descriptors in PID. They only used to restore previous state of PID.

Without -N, reredirect keep previous output opened which allow you to restore them. This will produce file descriptor leak if you call reredirect multiple times. You should use -N to close and forget previous output.

Redirect to your terminal or a command

This package also provide an utility called relink that allows to redirect output to current terminal. When relink exits (with Ctrl+C for example) original state is restored and command is detached.

For example:

relink 5453
relink 5453 | grep useful_line

relink maintains stderr from original command to stderr. So you can do things like:

relink 5453 > /dev/null

Internally, relink is just a small shell script that creates necessary context and call reredirect as necessary. It uses "named pipes". Using "named pipes", you can redirect output of your target to another command (as a normal pipe):

First create a named pipe:

mkfifo /tmp/myfifo

Run reredirect to redirect your target to /tmp/myfifo:

reredirect -m /tmp/myfifo PID

Launch a command on this named pipe:

less  ./restore_$$PPID.cmd
	@echo Call sub makefile here
	@sh ./restore_$$PPID.cmd
	@echo No more in log file

Portability

reredirect is Linux-only. It uses ptrace to attach to the target and control it at the syscall level, so it is highly dependent on Linux's particular syscall API, syscalls, and terminal ioctl()s. A port to Solaris or BSD may be technically feasible, but would probably require significant re-architecting to abstract out the platform-specific bits.

reredirect works on i386, x86_64, and ARM. Ports to other architectures should be straightforward, and should in most cases be as simple as adding an arch/ARCH.h file and adding a clause to the ifdef ladder in ptrace.c.

ptrace_scope on Ubuntu Maverick and up

redirect depends on the ptrace system call to attach to the remote program. On Ubuntu Maverick and higher, this ability is disabled by default for security reasons. You can enable it temporarily by doing

# echo 0 > /proc/sys/kernel/yama/ptrace_scope

as root, or permanently by editing the file /etc/sysctl.d/10-ptrace.conf, which also contains more information about exactly what this setting accomplishes.

How does it work?

Reredirect acts as a debugger to take control of a running process (it uses ptrace syscall). Once it takes control of a running process, it uses classical calls to dup, and dup2 to change targets of file descriptors 1 and 2.

Basicly, to redirect to file, this pseudo code is executed:

orig_fd = 1;
save_fd = dup(1);
new_fd = open(file, O_WRONLY | O_CREAT, 0666);
dup2(new_fd, orig_fd);
close(new_fd);

and to restore state:

ret = dup2(save_fd, orig_fd);
close(save_fd);

Credits

reredirect was mainly written by Jérôme Pouiller . You can contact him for any questions or bug reports.

reredirect (and especially all ptrace layer) is based on reptyr programm. reptyr was written by Nelson Elhage .

URL

https://github.com/jerome-pouiller/reredirect

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言