用 Linux 的 watch 命令观察命令和任务
watch

> 
> 了解 watch 命令如何让你知道任务已完成或命令已执行。
> 
> 
> 


![](/data/attachment/album/202109/22/104541ddfgzpvud5ga55sp.jpg "Clock, pen, and notepad on a desk")


有很多时候，你需要等待一些事情的完成，比如：


* 一个文件的下载。
* 创建或解压一个 [tar](https://opensource.com/article/17/7/how-unzip-targz-file) 文件。
* 一个 [Ansible](https://opensource.com/resources/what-ansible) 作业。


其中一些进程有进度指示，但有时进程是通过一层抽象运行的，衡量进度的唯一方法是通过其副作用。其中一些可能是：


* 一个正在下载的文件不断增长。
* 一个从 tarball 中提取的目录被文件填满了。
* Ansible 作业构建了一个[容器](https://opensource.com/resources/what-docker)。


你可以用这样的命令查询所有这些：



```
$ ls -l downloaded-file
$ find . | wc -l
$ podman ps
$ docker ps

```

但是反复运行这些命令，即使是利用 [Bash 历史](https://opensource.com/article/20/6/bash-history-control) 和**向上箭头**的便利，也是很乏味的。


另一种方法是写一个小的 Bash 脚本来为你自动执行这些命令：



```
while :
do
 docker ps
 sleep 2
done

```

但这样的脚本写起来也会很繁琐。你可以写一个小的通用脚本，并将其打包，这样它就可以一直被你使用。幸运的是，其他开源的开发者已经有了这样的经验和做法。


那就是 `watch` 这个命令。


### 安装 watch


`watch` 命令是 [procps-ng 包](https://opensource.com/article/21/8/linux-procps-ng)的一部分，所以如果你是在 Linux 上，你已经安装了它。


在 macOS 上，使用 [MacPorts](https://opensource.com/article/20/11/macports) 或 [Homebrew](https://opensource.com/article/20/6/homebrew-mac) 安装 `watch`。在 Windows 上，使用 [Chocolatey](https://opensource.com/article/20/3/chocolatey)。


### 使用 watch


`watch` 命令定期运行一个命令并显示其输出。它有一些文本终端的特性，所以只有最新的输出才会出现在屏幕上。


最简单的用法是：`watch <command>`。


例如，在 `docker ps` 命令前加上 `watch`，就可以这样操作：



```
$ watch docker ps

```

用 `watch` 命令，以及一些创造性的 Unix 命令行技巧，可以生成临时的仪表盘。例如，要计算审计事件：



```
$ watch 'grep audit: /var/log/kern.log |wc -l'

```

在最后一个例子中，如果有一个可视化的指示，表明审计事件的数量发生了变化，这可能是有用的。如果变化是预期的，但你想让一些东西看起来“不同”，`watch --differences` 就很好用。它可以高亮显示与上次运行的任何差异。如果你在多个文件中搜索，这一点尤其有效，所以你可以很容易地看到哪个文件发生了变化。


如果没有预期的变化，你可以使用 `watch --differences=permanent` 要求它们被“永久”高亮显示，以便知道哪些变化需要调查。这通常是更有用的。


### 控制频率


最后，有时该命令可能是资源密集型的，不应运行得太频繁。`-n` 参数控制频率。`watch` 默认使用 2 秒间隔，但是 `watch -n 10` 可能适合于资源密集型的情况，比如在子目录的任何文件中搜索一个模式：



```
$ watch -n 10 'find . -type f | xargs grep suspicious-pattern'

```

### 用 watch 观察一个命令


`watch` 命令对于许多临时性的系统管理任务非常有用，在这些任务中，你需要在没有进度条的情况下等待一些耗时的步骤，然后再进入下一个步骤。尽管这种情况并不理想，但 `watch` 可以使情况稍微好转。它让你有时间为工作做回顾性笔记！"。下载 [备忘录](https://opensource.com/downloads/watch-cheat-sheet)，让有用的语法和选项触手可及。。




---


via: <https://opensource.com/article/21/9/linux-watch-command>


作者：[Moshe Zadka](https://opensource.com/users/moshez) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
There are many times when you need to wait for something to finish, such as:

Some of these processes have some sort of progress indication, but sometimes the process is run through a layer of abstraction, and the only way to measure the progress is through its side effects. Some of these might be:

- A file being downloaded keeps growing.
- A directory extracted from a tarball fills up with files.
- The Ansible job builds a
[container](https://opensource.com/resources/what-docker).

You can query all of these things with commands like these:

```
$ ls -l downloaded-file
$ find . | wc -l
$ podman ps
$ docker ps
```


But running these commands over and over, even if it is with the convenience of [Bash history](https://opensource.com/article/20/6/bash-history-control) and the **Up Arrow**, is tedious.

Another approach is to write a little Bash script to automate these commands for you:

```
while :
do
docker ps
sleep 2
done
```


But such scripts can also become tedious to write. You could write a little generic script and package it, so it's always available to you. Luckily, other open source developers have already been there and done that.

The result is the command `watch`

.


## Installing watch

The `watch`

command is part of the [ procps-ng package](https://opensource.com/article/21/8/linux-procps-ng), so if you're on Linux, you already have it installed.

On macOS, install `watch`

using [MacPorts](https://opensource.com/article/20/11/macports) or [Homebrew](https://opensource.com/article/20/6/homebrew-mac). On Windows, use [Chocolatey](https://opensource.com/article/20/3/chocolatey).

## Using watch

The `watch`

command periodically runs a command and shows its output. It has some text-terminal niceties, so only the latest output is on the screen.

The simplest usage is: `watch <command>`

.

For example, prefixing the

command with `docker ps``watch`

works like this:

`$ watch docker ps`


The `watch`

command, and a few creative Unix command-line tricks, can generate ad-hoc dashboards. For example, to count audit events:

`$ watch 'grep audit: /var/log/kern.log |wc -l'`


In the last example, it is probably useful if there's a visual indication that the number of audit events changed. If change is expected, but you want something to look "different," `watch --differences`

works well. It highlights any differences from the last run. This works especially well if you are grepping in multiple files, so you can easily see which one changed.

If changes are not expected, you can ask for them to be highlighted "permanently" to know which ones to investigate by using `watch --differences=permanent`

. This is often more useful.

## Controlling frequency

Finally, sometimes the command might be resource-intensive and should not be run too frequently. The `-n`

parameter controls the frequency. Watch uses two seconds by default, but `watch -n 10`

might be appropriate for something more resource-intensive, like grepping for a pattern in any file in a subdirectory:

`$ watch -n 10 'find . -type f | xargs grep suspicious-pattern'`


## Watch a command with watch

The `watch`

command is useful for many ad-hoc system administration tasks where you need to wait for some time-consuming step, without a progress bar, before moving on to the next one. Though this is not a great situation to be in, `watch`

can make it slightly better—and give you time to start working on those notes for the retrospective! Download the ** cheat sheet **to keep helpful syntax and options close at hand.

## Comments are closed.