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 #就可以看到在本終端顯示輸出了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM