代码改变世界

linux tee命令 ,script命令

2021-06-07 13:51  youxin  阅读(522)  评论(0编辑  收藏  举报

你想要输出的信息也同时能写入文件。这个时候,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    即可完成接管
————————————————