Linux watch 命令周期性的执行程序,并全屏显示执行结果

Linux watch Command

Sometimes, while working on the Linux command line, you might want to execute a command repeatedly so as to track any change in output. Well, you'll be happy to know there exists a command line utility that lets you do this. The tool in question is Watch, and in this tutorial, we will discuss some of its basic features using some easy to understand examples.

 

But before we do that, it's worth mentioning that all examples used in this article have been tested on Ubuntu 16.04 LTS.

Linux watch command

As already mentioned, the watch utility lets you execute a command repeatedly. Following is the syntax of this command:

watch [options] command

And here's how the tool's man page describes it:

watch runs command repeatedly, displaying its output and errors (the
first screenfull). This allows you to watch the program output change
over time. By default, the program is run every 2 seconds. By
default, watch will run until interrupted.

Following are some Q&A-styled examples that should give you a good idea on how the 'watch' command works.

Q1. How watch command works?

The baisc usage of watch is very easy - all you have to do is to run the tool with a command name as input. For example:

watch date

The above command produced the following output on my system:

How watch command works?

The first line of the output tells the output of the command 'date' is being refreshed after every 2 seconds (that's the default time interval), followed by the current date/time of the system. The second line is the actual output of the 'date' command that's getting refreshed after every couple of seconds.

Q2. How to make watch highlight difference between updates?

As already explained in the previous section, the output of the command being tracked is regularly updated (after 2 seconds by default). In case you want, you can also make 'watch' highlight the part of output that's changing between updates.

This you can do using the -d command line option. For example:

watch -d date

And here's how the watch command highlights the changes now:

How to make watch highlight difference between updates?

Q3. How to customize update interval?

As already mentioned, the default update interval is 2 seconds. However, if you want, you can change this value by using the -n command line option, which requires you to pass the new interval value as its input.

For example:

watch -n 5 date

This command will make sure the output gets updated after every 5 seconds. Please note that intervals less that 0.1 seconds aren't allowed.

Note: To make sure the interval gap is honored very precisely, use the -p command line option. Here's how the man page describes this option:

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 continu?
ously increase.

Q4. How to turn off the header in output?

If you want, you can also ask 'watch' to turn off the header showing the interval, command, and current
time at the top of the display, as well as the following blank line. This can be done using the -t command line option.

For example:

watch -t date

Here's the output produced by the command above:

How to turn off the header in output?

So you can see the header was completely stripped off.

Q5. How to make watch exit when the output changes?

If you want the 'watch' command to exit after a change in output is detected, use the -g command line option.

For example:

watch -g free

So anytime the memory consumption in the system changes, the watch command will exit.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值