linux命令大全:watch

2019-02-0910:53:12服务器及运维Comments2,813 views字数 1218阅读模式

watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行。在Linux下,watch是周期性的执行下个程序,并全屏显示执行结果。你可以拿他来监测你想要的一切命令的结果变化,比如 tail 一个 log 文件,ls 监测某个文件的大小变化,看你的想象力了!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

1.命令格式

watch[参数][命令]文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

2.命令功能

可以将命令的输出结果输出到标准输出设备,多用于周期性执行命令/定时执行命令文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

3.命令参数

  • -n--interval watch缺省每2秒运行一下程序,可以用-n或-interval来指定间隔的时间。
  • -d或—differences 用-d或—differences 选项watch 会高亮显示变化的区域。 而-d=cumulative选项会把变动过的地方(不管最近的那次有没有变动)都高亮显示出来。
  • -t 或-no-title 会关闭watch命令在顶部的时间间隔,命令,当前时间的输出。
  • -h, —help 查看帮助文档

4.使用实例

实例1:

命令:每隔一秒高亮显示网络链接数的变化情况文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

watch -n 1 -d netstat -ant
Shell

输出结果:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

Every 1.0s: netstat -ant                                Mon Feb 27 20:49:38 2017

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 192.168.0.210:22        192.168.0.5:51577       ESTABLISHED
tcp6       0      0 :::80                   :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN
Shell

说明:其它操作:
切换终端: Ctrl+x
退出watchCtrl+g文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

实例2:每隔一秒高亮显示http链接数的变化情况

命令:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

watch -n 1 -d 'pstree|grep http'
Shell

说明:
每隔一秒高亮显示http链接数的变化情况。 后面接的命令若带有管道符,需要加’’将命令区域归整。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

实例3:实时查看模拟攻击客户机建立起来的连接数

命令:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

watch 'netstat -an | grep:21 | \ grep<客户机的IP>| wc -l'
Shell

实例4:监测当前目录中 scf’ 的文件的变化

命令:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

watch -d 'ls -l|grep scf'
Shell

实例5:10秒一次输出系统的平均负载

命令:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

watch -n 10 'cat /proc/loadavg'
Shell

输出结果如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html

Every 10.0s: cat /proc/loadavg                          Mon Feb 27 20:54:45 2017

0.01 0.04 0.05 1/132 6335
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9502.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/9502.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定