watch命令用法举例

watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行。
在Linux下,watch是周期性的执行下个程序,并全屏显示执行结果。
你可以拿他来监测你想要的一切命令的结果变化,比如tail 1个log文件,ls监测某个文件的大小变化。

1.命令格式:watch[参数][命令]

2.命令功能:可以将命令的输出结果输出到标准输出设备,多用于周期性执行命令/定时执行命令

3.命令参数:

-n或--interval  watch缺省每2秒运行一下程序,可以用-n或-interval来指定间隔的时间。

-d或--differences  用-d或--differences 选项watch 会高亮显示变化的区域。 而-d=cumulative选项会把变动过的地方(不管最近的那次有没有变动)都高亮显示出来。

-t 或-no-title  会关闭watch命令在顶部的时间间隔,命令,当前时间的输出。

-h, --help 查看帮助文档

其它操作:
切换终端: Ctrl+x
退出watch:Ctrl+g

4.使用实例:
4.1 每隔一秒高亮显示网络链接数的变化情况: watch -n 1 -d netstat -ant
[moiaopr@CNSZ443239 ~]$ watch -n 1 netstat -ant

tcp        0      0 10.14.192.128:42744         10.14.58.94:1521            TIME_WAIT
tcp        0      0 10.14.192.128:42747         10.14.58.94:1521            TIME_WAIT
Every 1.0s: netstat -ant                                              Mon Nov 21 15:24:25 2016

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
tcp        0      0 10.14.192.128:42777         10.14.58.94:1521            TIME_WAIT 
tcp        0      0 10.14.192.128:42787         10.14.58.94:1521            TIME_WAIT
tcp        0      0 10.14.192.128:22            10.50.123.18:59642          ESTABLISHED
tcp        0      0 10.14.192.128:42817         10.14.58.94:1521            TIME_WAIT  
tcp        0      0 10.14.192.128:22            10.13.224.137:53426         ESTABLISHED
tcp        0      0 10.14.192.128:42827         10.14.58.94:1521            TIME_WAIT   

4.2 每隔一秒高亮显示http链接数的变化情况: watch -n 1 -d 'pstree|grep http'

[moiaopr@CNSZ443239 ~]$ watch -n 1 -d 'pstree|grep http'
Every 1.0s: pstree|grep http                                                   Mon Nov 21 15:26:17 2016

4.3 监测当前目录中test01.txt文件的变化:  watch -d 'ls -l|grep test01.txt'

[moiaopr@CNSZ443239 ~]$ watch -d 'ls -l|grep test01.txt'

Every 2.0s: ls -l|grep test01.txt                                                                           Mon Nov 21 15:32:07 2016

-rw-r----- 1 moiaopr dstage     107 11月 17 17:07 test01.txt

4.4 10秒一次输出系统的平均负载: watch -n 10 'cat /proc/loadavg'

[moiaopr@CNSZ443239 ~]$ watch -n 10 'cat /proc/loadavg'                                                                            

Every 10.0s: cat /proc/loadavg                                                                              Mon Nov 21 15:33:12 2016

0.72 0.29 0.16 1/505 27469


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值