TranslateProject/published/201401/Command Line Basics – watch.md
wxy 1ee2fc7cbe renamed: 10 Lesser Known Useful Linux Commands- Part V.md -> 201401/10 Lesser Known Useful Linux Commands- Part V.md
renamed:    10 Useful Chaining Operators in Linux with Practical Examples.md -> 201401/10 Useful Chaining Operators in Linux with Practical Examples.md
	renamed:    10 basic examples of linux netstat command.md -> 201401/10 basic examples of linux netstat command.md
	renamed:    12 Advanced Commands For Linux Server Admins!.md -> 201401/12 Advanced Commands For Linux Server Admins!.md
	renamed:    14 New Linux Distros That Were Introduced In 2013.md -> 201401/14 New Linux Distros That Were Introduced In 2013.md
	renamed:    15 Basic MySQL Interview Questions for Database Administrators.md -> 201401/15 Basic MySQL Interview Questions for Database Administrators.md
	renamed:    "2013\357\274\232The Golden Year for Linux \342\200\223 10 Biggest Linux Achievements.md" -> "201401/2013\357\274\232The Golden Year for Linux \342\200\223 10 Biggest Linux Achievements.md"
	renamed:    2014--The year of the Linux car.md -> 201401/2014--The year of the Linux car.md
	renamed:    5 Things To Love And Hate About Ubuntu 13.10.md -> 201401/5 Things To Love And Hate About Ubuntu 13.10.md
	renamed:    8 Interesting Linux Tips And Tricks!.md -> 201401/8 Interesting Linux Tips And Tricks!.md
	renamed:    CentOS 6.5 desktop installation guide with screenshots.md -> 201401/CentOS 6.5 desktop installation guide with screenshots.md
	renamed:    "Command Line Basics \342\200\223 watch.md" -> "201401/Command Line Basics \342\200\223 watch.md"
	renamed:    Configure Your Browser To Use Tor On Ubuntu or Debian or Linux Mint.md -> 201401/Configure Your Browser To Use Tor On Ubuntu or Debian or Linux Mint.md
	renamed:    Daily Ubuntu Tips - Mount Partitions In Ubuntu From Your Desktop GUI.md -> 201401/Daily Ubuntu Tips - Mount Partitions In Ubuntu From Your Desktop GUI.md
	renamed:    "Daily Ubuntu Tips \342\200\223 Do Nothing When Laptop Lid Is Closed.md" -> "201401/Daily Ubuntu Tips \342\200\223 Do Nothing When Laptop Lid Is Closed.md"
	renamed:    "Daily Ubuntu Tips \342\200\224 Install VMware Workstation In Ubuntu.md" -> "201401/Daily Ubuntu Tips \342\200\224 Install VMware Workstation In Ubuntu.md"
	renamed:    "Daily Ubuntu Tips \342\200\224 Windows Disk Management Equivalent In Ubuntu.md" -> "201401/Daily Ubuntu Tips \342\200\224 Windows Disk Management Equivalent In Ubuntu.md"
	renamed:    "Gnu--toward the post-scarcity world \342\200\223 the Free Software Column.md" -> "201401/Gnu--toward the post-scarcity world \342\200\223 the Free Software Column.md"
	renamed:    How to Dual Boot Ubuntu and Windows Properly.md -> 201401/How to Dual Boot Ubuntu and Windows Properly.md
	renamed:    "How to Install and Configure UFW \342\200\223 An Un-complicated FireWall in Debian or Ubuntu.md" -> "201401/How to Install and Configure UFW \342\200\223 An Un-complicated FireWall in Debian or Ubuntu.md"
	renamed:    How to Upgrade to GNOME 3.10 in Ubuntu 13.10.md -> 201401/How to Upgrade to GNOME 3.10 in Ubuntu 13.10.md
	renamed:    How to install and configure Nagios on Linux.md -> 201401/How to install and configure Nagios on Linux.md
	renamed:    How to set password policy on Linux.md -> 201401/How to set password policy on Linux.md
	renamed:    How to stitch photos together on Linux.md -> 201401/How to stitch photos together on Linux.md
	renamed:    How to upgrade MySQL server on Debian or Ubuntu.md -> 201401/How to upgrade MySQL server on Debian or Ubuntu.md
	renamed:    Juju ice-cream icon design.md -> 201401/Juju ice-cream icon design.md
	renamed:    Linus Torvalds Releases Last Linux Kernel 3.13 RC for 2013.md -> 201401/Linus Torvalds Releases Last Linux Kernel 3.13 RC for 2013.md
	renamed:    Linus Torvalds Says All Contributor License Agreements Are Broken.md -> 201401/Linus Torvalds Says All Contributor License Agreements Are Broken.md
	renamed:    Linux free Command - Display Free and used Memory in the System.md -> 201401/Linux free Command - Display Free and used Memory in the System.md
	renamed:    Linux id Command - Print user ID and group ID information.md -> 201401/Linux id Command - Print user ID and group ID information.md
	renamed:    Linux is Everywhere. We show you exactly where.md -> 201401/Linux is Everywhere. We show you exactly where.md
	renamed:    Linux lsusb Command to Print information about USB on System.md -> 201401/Linux lsusb Command to Print information about USB on System.md
	renamed:    Linux vmstat Command - Tool to Report Virtual Memory Statistics.md -> 201401/Linux vmstat Command - Tool to Report Virtual Memory Statistics.md
	renamed:    "Linux who command \342\200\223 Displays who is on the system.md" -> "201401/Linux who command \342\200\223 Displays who is on the system.md"
	renamed:    "Move Dropbox\342\200\231s Folder To An External Drive In Ubuntu.md" -> "201401/Move Dropbox\342\200\231s Folder To An External Drive In Ubuntu.md"
	renamed:    New Ubuntu 14.04 Icons Are Drop-Dead Gorgeous, Might Not Arrive in Desktop Version.md -> 201401/New Ubuntu 14.04 Icons Are Drop-Dead Gorgeous, Might Not Arrive in Desktop Version.md
	renamed:    Our Top 10 Linux Applications of 2013.md -> 201401/Our Top 10 Linux Applications of 2013.md
	renamed:    Setup your personal Cloud server in minutes using ownCloud On RHEL, CentOS, Scientific Linux 6.5.md -> 201401/Setup your personal Cloud server in minutes using ownCloud On RHEL, CentOS, Scientific Linux 6.5.md
	renamed:    Software May Be Eating The World, But Open Source Software Is Eating Itself.md -> 201401/Software May Be Eating The World, But Open Source Software Is Eating Itself.md
	renamed:    The Debian Administrator's Handbook updated for Debian 7 Wheezy published and freely available for download.md -> 201401/The Debian Administrator's Handbook updated for Debian 7 Wheezy published and freely available for download.md
	renamed:    The Fedora Project Will No Longer Name Its Linux Distributions.md -> 201401/The Fedora Project Will No Longer Name Its Linux Distributions.md
	renamed:    The Genius Of Linux Is Community, Not Technology.md -> 201401/The Genius Of Linux Is Community, Not Technology.md
	renamed:    Top 10 Linux Distros For Hackers!.md -> 201401/Top 10 Linux Distros For Hackers!.md
	renamed:    Tunnel SSH Connections Over SSL Using 'Stunnel' On Debian 7  Ubuntu 13.10.md -> 201401/Tunnel SSH Connections Over SSL Using 'Stunnel' On Debian 7  Ubuntu 13.10.md
	renamed:    Ubuntu Stores Your Wi-Fi Passwords By Default!.md -> 201401/Ubuntu Stores Your Wi-Fi Passwords By Default!.md
	renamed:    Ubuntu Will Reach True Convergence Before Microsoft, Says Shuttleworth.md -> 201401/Ubuntu Will Reach True Convergence Before Microsoft, Says Shuttleworth.md
	renamed:    Understanding Linux cd Command with Examples.md -> 201401/Understanding Linux cd Command with Examples.md
	renamed:    look--Linux Command To Verify Spellings And Display Lines Beginning With A String.md -> 201401/look--Linux Command To Verify Spellings And Display Lines Beginning With A String.md
2014-02-03 20:55:51 +08:00

66 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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](http://tuxtweaks.com/wp-content/uploads/2013/12/Watch_01_wm.png)
*使用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命令](http://tuxtweaks.com/wp-content/uploads/2013/12/Watch_02_wm.png)
*带管道的watch命令*
### 将watch作为时钟 ###
就像你现在已经注意到的,**watch**执行后会在你的终端的右上角显示时间。我们可以通过传给watch一个空的命令参数来把它作为一个简单的时钟。 我们可以将一个空格包含在引号中来作为一个空的参数。
watch -n 1 " "
如你所见,这给予这个命令另外一个意义,**手表(watch)**。你可以把它作为你的腕表。
现在你知道如何使用Linux的watch命令。你要用它处理什么重复任务?
--------------------------------------------------------------------------------
via: http://tuxtweaks.com/2013/12/linux-watch-command/
译者:[geekpi](https://github.com/geekpi) 校对:[Mr小眼儿](http://blog.csdn.net/tinyeyeser)
本文由 [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/