mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-03-24 02:20:09 +08:00
Merge pull request #565 from geekpi/master
[Translating] Command Line Basics - watch
This commit is contained in:
commit
fbc1bd1ff4
@ -1,65 +0,0 @@
|
||||
Command Line Basics – watch
|
||||
================================================================================
|
||||
There are several log files in a Linux system. Keeping an eye on these log files can be one of the important tasks of a Linux System administrator. You can easily view the end of a log file [using the tail command][1]. But if you want to monitor that file all day long it's pretty tedious to enter the tail command every few minutes to check on that log file. You could write a short [script with an infinite loop][2] to check the file periodically, but it turns out that there is already a program to handle repetitive tasks for you.
|
||||
|
||||
### The Linux watch Command ###
|
||||
|
||||
The **watch** command in Linux provides a way to handle repetitive tasks. By default **watch** will repeat the command that follows it every two seconds. As you can imagine, watch is a great tool to keep an eye on log files. Here's an example.
|
||||
|
||||
watch tail /var/log/syslog
|
||||
|
||||
In order to stop the command execution, just use the standard kill sequence, **[Ctrl]+C**.
|
||||
|
||||

|
||||
*Using the Linux watch command to monitor the syslog*
|
||||
|
||||
You can change the time interval by issuing the **-n** switch and specifying the interval in seconds. To check the log file every 10 seconds, try this.
|
||||
|
||||
watch -n 10 tail /var/log/syslog
|
||||
|
||||
### The Linux watch Command with a Pipe ###
|
||||
|
||||
The **watch** command isn't limited to viewing log files. It can be used to repeat any command you give it. If you have your system [set up to monitor the CPU temperature][3], you can use **watch** to view that with the **sensors** command.
|
||||
|
||||
watch -n 1 sensors
|
||||
|
||||
The output on my netbook looks like this:
|
||||
|
||||
acpitz-virtual-0
|
||||
Adapter: Virtual device
|
||||
temp1: +45.0°C (crit = +100.0°C)
|
||||
|
||||
I'd like to filter this output to only show the temperature output without all of the rest.
|
||||
|
||||
I can use this command to view it one time.
|
||||
|
||||
sensors | grep temp | awk '{ print $2 }'
|
||||
|
||||
Keep in mind that the watch command will repeat the first command that is sees. Care must be taken when pipelining one command to the next. This can be managed by placing your command pipeline inside quotes.
|
||||
|
||||
watch -n1 "sensors | grep temp | awk '{ print $2 }'"
|
||||
|
||||

|
||||
*Using the Linux watch command with a pipe*
|
||||
|
||||
### Use watch as a clock ###
|
||||
|
||||
As you've probably noticed by now, the **watch** command shows the time that the command was executed in the upper right corner of the terminal window. We can use **watch** as a simple clock by passing an empty command line argument. We can just enclose a space in quotes to act as the empty command.
|
||||
|
||||
watch -n 1 " "
|
||||
|
||||
So you can see, this gives another meaning for the command name, **watch**. You can use it just like your wrist watch.
|
||||
|
||||
So now you know how to use the Linux watch command. What repetitive tasks will you use it to handle?
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://tuxtweaks.com/2013/12/linux-watch-command/
|
||||
|
||||
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
||||
[1]:http://tuxtweaks.com/2011/02/command-line-basics-head-and-tail/
|
||||
[2]:http://tuxtweaks.com/2012/01/creating-a-terminal-window-clock/
|
||||
[3]:http://tuxtweaks.com/2008/08/how-to-control-fan-speeds-in-ubuntu/
|
65
translated/Command Line Basics – watch.md
Normal file
65
translated/Command Line Basics – watch.md
Normal file
@ -0,0 +1,65 @@
|
||||
Linux 基础命令 – watch
|
||||
================================================================================
|
||||
linux系统里有一些日志文件。观察这些日志文件是系统管理员的一个重要任务。你可以很方便地[使用tail命令][1]观察它们。但是如果你想要长时间监视这些文件,每几分钟使用tail检查那些日志文件是一件很乏味的事情。你会写一个短小的[无限循环的脚本][2]来周期性地检查文件,但是它会导致已经有程序为你处理了重复的任务。
|
||||
|
||||
### Linux watch 命令 ###
|
||||
|
||||
Linux中的**watch** 命令提供了一种方式处理重复的任务。默认上**watch**会每2秒重复执行命令。就如你想的,watch是一个很好的观察log文件的工具。下面是一个例子。
|
||||
|
||||
watch tail /var/log/syslog
|
||||
|
||||
想要停止命令的执行,只要使用标准的kill流程, **[Ctrl]+C**。
|
||||
|
||||

|
||||
*使用Linux watch命令监测syslog*
|
||||
|
||||
你可以使用**-n**开关改变并指定时间间隔。要想每10秒检测日志文件,试试这个。
|
||||
|
||||
watch -n 10 tail /var/log/syslog
|
||||
|
||||
### 带有管道的watch命令 ###
|
||||
|
||||
**watch**没有被限制浏览日志文件。它可以用来重复你给它的任何命令。如果你要[监测CPU的温度][3],你可以使用**watch**后跟上**sensord**命令来查看。
|
||||
|
||||
watch -n 1 sensors
|
||||
|
||||
我电脑上的输出看上去就像这样:
|
||||
|
||||
acpitz-virtual-0
|
||||
Adapter: Virtual device
|
||||
temp1: +45.0°C (crit = +100.0°C)
|
||||
|
||||
我想过滤一下这个输出来只显示温度而不显示其他的。
|
||||
|
||||
我可以使用这个命令来查看
|
||||
|
||||
sensors | grep temp | awk '{ print $2 }'
|
||||
|
||||
记住,watch命令会重复它后面的第一个命令。必须要注意命令后面跟上管道的情况。你可以将你的命令放在引号里面来管理。
|
||||
|
||||
watch -n1 "sensors | grep temp | awk '{ print $2 }'"
|
||||
|
||||

|
||||
*带管道的watch命令*
|
||||
|
||||
### 将watch作为时钟 ###
|
||||
|
||||
就像你现在已经注意到的,**watch**执行后会在你的终端的右上角显示时间。我们可以通过传给watch一个空的命令参数来把它作为一个简单的时钟。 我们可以将一个空格包含在引号中来作为一个空的参数。
|
||||
|
||||
watch -n 1 " "
|
||||
|
||||
如你所见,这给予这个命令另外一个意义,**手表(watch)**。你可以把它作为你的腕表。
|
||||
|
||||
现在你知道如何使用Linux的watch命令。你要用它处理什么重复任务?
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://tuxtweaks.com/2013/12/linux-watch-command/
|
||||
|
||||
译者:[geekpi](https://github.com/geekpi) 校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
||||
[1]:http://tuxtweaks.com/2011/02/command-line-basics-head-and-tail/
|
||||
[2]:http://tuxtweaks.com/2012/01/creating-a-terminal-window-clock/
|
||||
[3]:http://tuxtweaks.com/2008/08/how-to-control-fan-speeds-in-ubuntu/
|
Loading…
Reference in New Issue
Block a user