zoukankan      html  css  js  c++  java
  • 每天一个linux命令:tail(16)

    tail

    tail命令用于输入文件中的尾部内容,不指定文件时,作为输入信息进行处理。tail命令默认在屏幕上显示指定文件的末尾10行。命令从指定点开始将文件写到标准输出,使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把文件里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容 ,常用于查看日志 。常用查看日志文件。

    格式

    tail [参数] [文件]

    参数选项

    参数 备注
    -f 显示文件最新追加的内容,循环读取
    -q 不显示处理信息,当有多个文件参数时,不输出各个文件名
    -v 显示详细的处理信息,当有多个文件参数时,总是输出各个文件名
    -c 显示的字节数
    -n 显示行数
    --pid 与-f合用,表示在进程ID,PID死掉之后结束
    -s, -s或--sleep-interal=<秒数 常与“-f”选项连用,指定监视文件变化时间隔的秒数

    实例

    • 显示文件末尾内容

      命令: **tail -n 5 myFile **

    [root@VM_0_9_centos ~]# cat myFile
    this is line 1;
    this is line 2;
    this is line 3;
    tihs is line 4;
    this is line 5;
    this is line 6;
    this is line 7;
    this is line 8;
    this is line 9;
    this is line 10;
    this is line 11;
    this is line 12;
    this is line 13;
    this is line 14;
    [root@VM_0_9_centos ~]# tail -n -5 myFile
    this is line 10;
    this is line 11;
    this is line 12;
    this is line 13;
    this is line 14;
    [root@VM_0_9_centos ~]# 
    
    • 从第5行开始显示文件

      命令: **tail -n +5 myFile **

    [root@VM_0_9_centos ~]# tail -n +5 myFile
    this is line 5;
    this is line 6;
    this is line 7;
    this is line 8;
    this is line 9;
    this is line 10;
    this is line 11;
    this is line 12;
    this is line 13;
    this is line 14;
    
    • 循环查看文件内容

      命令: tail -f myLog.log

    [root@VM_0_9_centos ~]# ping www.baidu.com >> myLog.log &
    [1] 32614
    [root@VM_0_9_centos ~]# tail -f myLog.log 
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=54 time=2.74 ms
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=5 ttl=54 time=2.73 ms
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=6 ttl=54 time=2.81 ms
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=7 ttl=54 time=2.72 ms
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=8 ttl=54 time=2.73 ms
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=9 ttl=54 time=2.78 ms
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=10 ttl=54 time=2.76 ms
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=11 ttl=54 time=2.75 ms
    64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=12 ttl=54 time=2.72 ms
    ...
    

    相关

    nohup 与 & 的区别

    • nohup -- invoke a utility immune to hangups:运行命令忽略挂起信号

      & :是指后台运行

      nohup的功能和& 之间的功能并不相同。其中,nohup 可以使得命令永远运行下去和用户终端没有关系。当我们断开ssh 连接的时候不会影响他的运行。而& 表示后台运行。当ssh 断开连接的时候(用户退出或挂起的时候),命令也自动退出。可以把两者结合起来使用:

      当然我们可以把两者结合起来使用:

      nohup command &
      

      来实现命令的后台运行并且和用户终端没有关系。

    • 例子

      ping www.baidu.com >> myLog.log &
      

      ping www.baidu.com >> myLog.log任务放到后台 ,此时ctrl+c,该任务继续正常运行, 前台依然能够接收任何输入 ,因为对SIGINT信号免疫 。关闭shell退出当前session则该任务进程关闭,可见&的后台并不硬(因为对SIGHUP信号不免疫),通过jobs命令可以查询到当前任务

      nohup ping www.baidu.com >> myLog.log
      

      nohup的意思是忽略SIGHUP信号, 所以当运行ping www.baidu.com >> myLog.log的时候, 关闭shell, 那么该任务进程还是存在的(对SIGHUP信号免疫)。 但是,此时shell会关闭标准输入,前台不再能够接收任何输入(标准输入),重定向标准输出和标准错误到当前目录下的nohup.out文件 ,如果你直接在shell中用ctrl+c, 那么, 该任务进程也是会消失的(因为对SIGINT信号不免疫);

      #nohup ping www.baidu.com >> myLog.log &  这个会报错
      nohup ping www.baidu.com >> myLog.log 2>&1
      

      ping www.baidu.com >> myLog.log任务放到后台,但是依然可以使用标准输入,前台能够接收任何输入,重定向标准输出和标准错误到当前目录下的nohup.out文件,即使关闭shell退出当前session依然继续运行。

    查看当前的后台任务

    • jobs命令

      -l选项可显示所有任务的PID,jobs的状态可以是running, stopped,Terminated,但是如果任务被终止了(kill),shell从当前的shell环境已知的列表中删除任务的进程标识;也就是说,jobs命令显示的是当前shell环境中所起的后台正在运行或者被挂起的任务信息

    参考

  • 相关阅读:
    CF 461B Appleman and Tree
    POJ 1821 Fence
    NOIP 2012 开车旅行
    CF 494B Obsessive String
    BZOJ2337 XOR和路径
    CF 24D Broken robot
    POJ 1952 BUY LOW, BUY LOWER
    SPOJ NAPTIME Naptime
    POJ 3585
    CF 453B Little Pony and Harmony Chest
  • 原文地址:https://www.cnblogs.com/DiDi516/p/11794956.html
Copyright © 2011-2022 走看看