watch命令实现Linux监控

2 篇文章 0 订阅

watch命令简介

watch命令可以重复的执行命令,并且将其输出在命令行上,可以方便的实现一些简单的监控

 watch -n 1 cat 1.txt

上面的这个命令输出1.txt的内容,-n 1表示每隔一秒钟执行一次命令

在这里插入图片描述
右侧shell执行命令,左侧是watch的输出

我们可以看到先是输出了wdnmd,然后输出了nmsl,至此,我们很简单的实现了一个不断变化的文件的监控,但它是实用的——比如监控一个动态变化的log日志

另一个小例子:在所有进程中查找是否有node关键字的进程,每隔三秒刷新一次

 watch -n 3 "ps aux | grep node"

example

EXAMPLES
       To watch for mail, you might do
              watch -n 60 from
       To watch the contents of a directory change, you could use
              watch -d ls -l
       If you're only interested in files owned by user joe, you might use
              watch -d 'ls -l | fgrep joe'
       To see the effects of quoting, try these out
              watch echo $$
              watch echo '$$'
              watch echo "'"'$$'"'"
       To see the effect of precision time keeping, try adding -p to
              watch -n 10 sleep 1
       You can watch for your administrator to install the latest kernel with
              watch uname -r
       (Note that -p isn't guaranteed to work across reboots, especially in the face of ntpdate or other bootup time-changing mechanisms)

option

OPTIONS
       -d, --differences [permanent]
              Highlight the differences between successive updates.  Option will read optional argument that changes highlight to be  permanent,  allowing  to
              see what has changed at least once since first iteration.

       -n, --interval seconds
              Specify  update  interval.  The command will not allow quicker than 0.1 second interval, in which the smaller values are converted. Both '.' and
              ',' work for any locales.

       -p, --precise
              Make watch attempt to run command every interval seconds. Try it with ntptime and notice how the fractional seconds stays (nearly) the same,  as
              opposed to normal mode where they continuously increase.

       -t, --no-title
              Turn off the header showing the interval, command, and current time at the top of the display, as well as the following blank line.

       -b, --beep
              Beep if command has a non-zero exit.

       -e, --errexit
              Freeze updates on command error, and exit after a key press.

       -g, --chgexit
              Exit when the output of command changes.

       -c, --color
              Interpret ANSI color and style sequences.

       -x, --exec
              Pass command to exec(2) instead of sh -c which reduces the need to use extra quoting to get the desired effect.

       -h, --help
              Display help text and exit.

       -v, --version
              Display version information and exit.
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值