linux tee命令 ,script命令


你想要輸出的信息也同時能寫入文件。這個時候,tee 命令就有其用武之地了。

NAME
tee - read from standard input and write to standard output and files

SYNOPSIS
tee [OPTION]... [FILE]...

DESCRIPTION
Copy standard input to each FILE, and also to standard output.

 

tee:

-a, --append
append to the given FILEs, do not overwrite

-i, --ignore-interrupts
ignore interrupt signals

--help display this help and exit

--version
output version information and exit

If a FILE is -, copy again to standard output.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report tee translation bugs to <http://translationproject.org/team/>

AUTHOR
Written by Mike Parker, Richard M. Stallman, and David MacKenzie.

COPYRIGHT
Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
The full documentation for tee is maintained as a Texinfo manual. If the info and tee programs are properly installed at your site, the command

info coreutils 'tee invocation'

 

Linux tee命令用於讀取標准輸入的數據,並將其內容輸出成文件。

在執行Linux命令時,我們可以把輸出重定向到文件中,比如 ls >a.txt,

這時我們就不能看到輸出了,如果我們既想把輸出保存到文件中,又想在屏幕上看到輸出內容,就可以使用tee命令了。

tee命令讀取標准輸入,把這些內容同時輸出到標准輸出和(多個)文件中,tee命令可以重定向標准輸出到多個文件。要注意的是:在使用管道線時,前一個命令的標准錯誤輸出不會被tee讀取。

tee指令會從標准輸入設備讀取數據,將其內容輸出到標准輸出設備,同時保存成文件。

語法

tee [-ai][--help][--version][文件...]

參數

  • -a或--append  附加到既有文件的后面,而非覆蓋它.
  • -i或--ignore-interrupts  忽略中斷信號。
  • --help  在線幫助。
  • --version  顯示版本信息。

 

 

tee最基本的用法就是顯示輸出結果並且保存內容到文件中。下面例子使用free命令顯示系統內存使用信息,並使用tee命令將信息輸出到屏幕,並保存到文件mem.txt中。

[root@localhost ~]# free -h | tee mem.txt
              total        used        free      shared  buff/cache   available
Mem:           1.8G        164M        1.2G        9.6M        387M        1.5G
Swap:          2.0G          0B        2.0G

可以查看一下mem.txt文件,可以看到輸出內容已經保存到mem.txt里面了。

寫入到多個文件

tee可以寫入多個文件,每個文件之間使用空格分隔。

[root@localhost ~]# free -h | tee mem1.txt mem2.txt mem3.txt
              total        used        free      shared  buff/cache   available
Mem:           1.8G        165M        1.2G        9.6M        389M        1.5G
Swap:          2.0G          0B        2.0G

 

在已存在的文件底部追加內容

下面的例子使用選項-a在文件底部追加內容,不覆蓋原有內容。

[root@localhost ~]# free -h | tee -a mem.txt
              total        used        free      shared  buff/cache   available
Mem:           1.8G        165M        1.2G        9.6M        389M        1.5G
Swap:          2.0G          0B        2.0G

可以看到,在mem.txt文件底部追加了新的內容。

如果不想在屏幕輸出內容,可以使用>標准輸出符號,重定向到/dev/null中:

[root@localhost ~]# free -h | tee -a mem.txt > /dev/null 


 如何讓 tee 命令的輸出內容直接作為另一個命令的輸入內容?

使用 tee 命令,你不僅可以將輸出內容寫入文件,還可以把輸出內容作為另一個命令的輸入內容。比如說,下面的命令不僅會將文件名存入 output.txt 文件中,還會通過 wc 命令讓你知道輸入到 output.txt 中的文件數目。

  1. ls file* | tee output.txt | wc -l

 

 

多個命令的輸出都需要記錄,可以用script
  script這個命令很強大,可以記錄終端的所有輸出到相應的文件中
  看例子:
  [lhd@hongdi ~]$ script
  Script. started, file is typescript
  [lhd@hongdi ~]$ ls
  1.gtkrc-2.0 c.tar kmess-2.0alpha2.tar.gz secpanel-0.5.3-1.noarch.rpm
  2009 DownZipAction.php kmesslog secpanel-0.5.4-2.noarch.rpm
  [lhd@hongdi ~]$ exit
  exit
  Script. done, file is typescript
  [lhd@hongdi ~]$ cat typescript
  Script. started on 2009年02月08日 星期日 18時56分52秒
  [lhd@hongdi ~]$ ls
  1.gtkrc-2.0 c.tar kmess-2.0alpha2.tar.gz secpanel-0.5.3-1.noarch.rpm
  2009 DownZipAction.php kmesslog secpanel-0.5.4-2.noarch.rpm
  [lhd@hongdi ~]$ exit
  exit
  Script. done on 2009年02月08日 星期日 18時57分00秒
  說明:
  1,我們在啟動script時沒有指定文件名,它會自動記錄到當前目錄下一個名為 typescript的文件中。也可以用 -a參數 指定文件名
  例子:
  [lhd@hongdi ~]$ script. -a example.txt
  Script. started, file is example.txt
  此時終端的輸出內容被記錄到 example.txt這個文件中
  2,退出script時,用exit
  感到奇怪嗎?事實上script就是啟動了一個shell
  看一下ps auxfww 的信息就知道了
  lhd 17738 0.1 3.2 152028 33328 ? Sl 18:30 0:03 /usr/bin/konsole
  lhd 17740 0.0 0.1 6372 1720 pts/1 Ss 18:30 0:00 \_ /bin/bash
  lhd 17900 0.0 0.0 5344 628 pts/1 S 19:01 0:00 | \_ script
  lhd 17901 0.0 0.0 5348 464 pts/1 S 19:01 0:00 | \_ script
  lhd 17902 0.5 0.1 6372 1688 pts/2 Ss 19:01 0:00 | \_ bash -i
  3,查看typescript的內容,可以看到它同時記錄下了script的啟動和結束時間

  四,用script錄制並播放session的內容
  我們可以用 script把整個終端會話的所有操作和輸出錄制下來,然后再用scriptreplay進行播放。
  如果錄制時記錄下來了操作時的時間數據,那么播放時和操作時的使用時間完全相同。
  這個很有用吧,比如:我們可以把安裝軟件時編譯的過程記錄下來,然后給別人進行演示
  看例子:
  [lhd@hongdi ~]$ script. -t 2>example.time -a example.txt
  Script. started, file is example.txt
  [lhd@hongdi ~]$ ls
  說明: -t 2>example.time -t是把時間數據輸出到標准錯誤(standard error),所以我們使用 2>example.time 把數據轉向到 example.time這個文件當中
  如何播放所記錄的內容?
  第一步:安裝scriptreplay
  下載
   wget linux/utils/util-linux/util-linux-2.12r.tar.bz2">ftp://ftp.kernel.org/pub/linux/utils/util-linux/util-linux-2.12r.tar.bz2
  解壓
  tar -jxvf util-linux-2.12r.tar.bz2
  之后復制文件到系統的命令目錄中即可
  [root@hongdi 下載]# cp util-linux-2.12r/misc-utils/scriptreplay.pl /usr/bin/scriptreplay
  [root@hongdi 下載]# chmod 755 /usr/bin/scriptreplay
  備注: fedora 10的util-linux-ng-2.14.1-3.2.fc10.i386.rpm 此包中已包含 scriptreplay,已無需另行安裝
  第二步:播放所錄制的session內容
  [lhd@hongdi ~]$ scriptreplay example1.time example1.txt
  [lhd@hongdi ~]$ ls
  1.gtkrc-2.0 c.tar jeffray_lee@hotmail.com pass
  [lhd@hongdi ~]$ abcd
  bash: abcd: command not found

  [lhd@hongdi ~]$ exit
 
 
reptyr is a utility for taking an existing running program and attaching it to a new terminal.  


大致原理:
利用ptrace利器接上目標進程,通過系統調用dup2和ioctl將標准輸入輸出和控制終端改成目前終端。
 

在Linux中,運行一個需要很長時間運行的進程的時候,中斷它可能會很痛苦,因為你如果你以前遇到過這樣的情況,那么你真的需要得到Reptyr工具。

Reptyr是什么?

Reptyr工具是一個命令行實用程序,它從一個終端接收運行的進程,並將其遷移到另一個終端。換句話說,它重復一個過程。這個名字也讓人聯想到假終端 - 通常縮寫為假終端(如果您不知道)是一種允許兩個進程(主和從)彼此鏈接的工具。你在一個寫的是反映在另一個。

Reptyr如何工作?

使用Reptyr,您可以在工作中開始一個過程,並在家里完成(如果你還有其他與Reptyr工具類似的工具,如Screenify和Retty,您可能以前遇到過。然而,Reptry目前已被證明比競爭對手更好。它具有更少的錯誤,並且效率更高。這些其他工具的問題是進程例如,如果您使用Screenify,並嘗試使用這是因為它們在當前具有控制的終端上執行。如果您調整程序窗口大小或嘗試

S Reptyr如何正常工作?它的目標是我們的過程Reptyr也不同於其他進程,因為它改變了控制終端。您可以查看官方博客來獲取詳細信息,但它基本上涉及操縱Linux中的ioctl,TIOCSCTTY功能來更改控制過程。

Installation

在基於Ubuntu的發行版中,您可以使用apt-get安裝application:

sudo apt-get install reptyr使用Reptyr

將過程移動到新屏幕您還可以使用Reptyr以及GNU Screen軟件應用程序(或類似的終端多路復用器應用程序) ,將您的進程移動到新的屏幕會話。例如,如果您正在運行進程並需要重新啟動服務器,則可以將其傳輸到新的屏幕會話,以便在系統重新啟動后再次訪問它。聽起來很有用,對吧?那么你該怎么做呢?

首先,你需要暫停你需要遷移的過程。這可以通過不同的方式完成然后,暫停它后,您需要在后台恢復該過程。這個命令是你不知道的,就是

bg

Next,你需要從父進程中隔離子進程。這是因為父進程在重新啟動期間將被終止,並且您需要將其保留為子進程。要做到這一點,請使用下面的命令.:

disown name_of_process

它您使用以下命令啟動它.1010mh1112

screen

現在,您需要檢索過程請注意,您需要該命令的運行進程的PID。或者,您可以使用如果你知道PID,type:

 

使用reptyr在screen中接管執行中進程

 

reptyr下載地址https://www.ostechnix.com/reptyr-move-running-process-new-terminal/

當你開啟了一個占用很長時間的進程,比如wget一個大文件,但是沒有在screen等這種環境里打開,又不想占着一個窗口一直等待或者擔心ssh中斷導致進程退出,就可以使用reptyr了,使用方法也很簡單,開啟一個screen,在screen中執行

reptyr 進行pid    即可完成接管
———————————————— 


免責聲明!

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



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