linux 动态重定向 正在运行的程序 的输出
reredirect
- 动态重定向 正在运行的程序 的输出工具
下载redirect 源码:wget https://github.com/jerome-pouiller/reredirect/archive/master.zip
参考网站:https://github.com/jerome-pouiller/reredirect/
安装:make;make install
用法:
reredirect -m FILE PID #自动标准输出,标准错误输出重定向输出到file文件 标准输出->5 标准错误输出->3
reredirect -o FILE1 -e FILE2 PID # 标准输出和错误输出重定向到不同文件
reredirect -N -O 5 -E 3 5453(pid) #取消 重定向
例子:
- test.sh
test@ubuntu:~$ cat test.sh
#!/bin/bash
i=0
while((1))
do
sleep 1
let i+=1
echo $i
done
- 在后台运行test.sh:setsid ./test.sh &
- 重新打开终端查看进程:ps -ef | grep test.sh
root@ubuntu:/usr/src/reredirect/reredirect-master# ps -ef | grep test.sh
test 27998 1 0 17:47 ? 00:00:00 /bin/bash ./test.sh
root 28011 10142 0 17:47 pts/2 00:00:00 grep --color=auto test.sh
- 查看进程输入输出重定向文件:
root@ubuntu:/proc/27998/fd# ll
total 0
dr-x------ 2 test test 0 Dec 13 17:47 ./
dr-xr-xr-x 9 test test 0 Dec 13 17:47 ../
lrwx------ 1 test test 64 Dec 13 17:47 0 -> /dev/pts/1
lrwx------ 1 test test 64 Dec 13 17:49 1 -> /dev/pts/1
lrwx------ 1 test test 64 Dec 13 17:49 2 -> /dev/pts/1
lr-x------ 1 test test 64 Dec 13 17:49 255 -> /home/test/test.sh*
- 使用reredirect 重定向进程文件:reredirect -m /tmp/test 27998
root@ubuntu:/proc/27998/fd# reredirect -m /tmp/test 27998
# Previous state saved. To restore, use:
reredirect -N -O 5 -E 3 27998
root@ubuntu:/proc/27998/fd# ll
total 0
dr-x------ 2 test test 0 Dec 13 17:47 ./
dr-xr-xr-x 9 test test 0 Dec 13 17:47 ../
lrwx------ 1 test test 64 Dec 13 17:47 0 -> /dev/pts/1
lrwx------ 1 test test 64 Dec 13 17:49 1 -> /tmp/test
lrwx------ 1 test test 64 Dec 13 17:49 2 -> /tmp/test
lr-x------ 1 test test 64 Dec 13 17:49 255 -> /home/test/test.sh*
lrwx------ 1 test test 64 Dec 13 17:52 3 -> /dev/pts/1
lrwx------ 1 test test 64 Dec 13 17:52 5 -> /dev/pts/1
- 上面可以可以看到 1 2重定向到/tmp/test 文件下了,然后tail -f /tmp/test查看文件内容:
root@ubuntu:/proc/27998/fd# tail -f /tmp/test
598
599
600
601
602
603
-
此时原来的终端停止了输出
-
取消重定向:root@ubuntu:/proc/27998/fd# reredirect -N -O 5 -E 3 27998,此时原来的终端会继续输出
- 怎么重定向到例外个终端比如pts2
先在此终端连接/tmp/test文件到pts2的输出: ln -s /dev/pts2/2 /tmp/test
然后使用reredirect进行修改进程输出;reredirect -m /tmp/test 27998 #就可以看到在本终端显示输出了