linux(redirect):动态重定向“正在运行的程序“的输出

既然有缘看到此文章,希望它对您有用,请您耐心学习完,并打赏下作者:

 

前言: 使用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

肯定收获满满吧, 记得回去上头打赏下呦,祝你步步高升.

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑着嘉陵车的男孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值