既然有缘看到此文章,希望它对您有用,请您耐心学习完,并打赏下作者:
前言: 使用linux工具reredirect--动态重定向 正在运行的程序 的输出工具
1. 下载、编译、安装redirect 工具:
wget https://github.com/jerome-pouiller/reredirect/archive/master.zip
unzip master.zip
cd reredirect-master/
make
make install
2. redirect 工具用法:
reredirect -m FILE PID
#自动标准输出,标准错误输出重定向输出到file文件 标准输出->5 标准错误输出->3
reredirect -o FILE1 -e FILE2 PID
# 标准输出和错误输出重定向到不同文件
reredirect -N -O 5 -E 3 5453(pid)
#取消 重定向
3. reredirect工具使用
新建测试脚本: test.sh
内容如下:
#!/bin/bash
i=0
while((1))
do
sleep 1
let i+=1
echo $i
done
打开命令行终端一:
使用测试脚本:
chmod 775 ./test.sh
setsid ./test.sh &
会发现一直在命令行终端打印数字,关闭终端,
打开命令行终端二:
执行ps -ef | grep test.sh
[root@localhost log]# ps -ef | grep test.sh
root 3250 1 0 10:26 ? 00:00:00 /bin/bash ./test.sh
root 3310 2991 0 10:27 pts/0 00:00:00 grep --color=auto test.sh
(发现test.sh脚本依然在后台进行,并且其进程id是3250)
查看进程输入输出重定向文件:
[root@localhost log]# ll /proc/3250/fd/
total 0
lrwx------. 1 root root 64 Jul 12 10:26 0 -> /dev/pts/1 (deleted)
lrwx------. 1 root root 64 Jul 12 10:27 1 -> /dev/pts/1 (deleted)
lrwx------. 1 root root 64 Jul 12 10:27 2 -> /dev/pts/1 (deleted)
lr-x------. 1 root root 64 Jul 12 10:27 255 -> /home/reredirect_tool/test.sh
使用reredirect 重定向进程文件:reredirect -m /tmp/test 3250
继续查看:
[root@localhost log]# reredirect -m /tmp/test 3250
# Previous state saved. To restore, use:
reredirect -N -O 5 -E 3 3250
[root@localhost log]# ll /proc/3250/fd/
total 0
lrwx------. 1 root root 64 Jul 12 10:26 0 -> /dev/pts/1 (deleted)
lrwx------. 1 root root 64 Jul 12 10:27 1 -> /tmp/test
lrwx------. 1 root root 64 Jul 12 10:27 2 -> /tmp/test
lr-x------. 1 root root 64 Jul 12 10:27 255 -> /home/reredirect_tool/test.sh
lrwx------. 1 root root 64 Jul 12 10:30 3 -> /dev/pts/1 (deleted)
lrwx------. 1 root root 64 Jul 12 10:30 5 -> /dev/pts/1 (deleted)
[root@localhost log]#
查看重定向后的文件内容,一直在增加
[root@localhost log]# tail -f /tmp/test
261
262
263
264
265
266
267
268
269
270
271
272
273
也可以用下面这种方式给一个进程的标准输出数据:
echo "msg serv test data" >> /proc/3250/fd/1
测试完毕关闭进程,删除/tmp/test文件:
[root@localhost log]# kill -9 3250
[root@localhost log]# rm -f /tmp/test
肯定收获满满吧, 记得回去上头打赏下呦,祝你步步高升.