回到手册索引

命令用途

watch 命令用于定期(默认2秒间隔)执行指定命令,并全屏显示输出结果,适合监控命令输出的动态变化。

常用用法示例

  1. 默认间隔(2秒)执行命令

    1
    2
    3
    watch date
    Every 2.0s: date hostname: Wed Jun 12 15:30:00 2024
    Wed Jun 12 15:30:00 CST 2024

    实时显示当前时间,每2秒刷新一次。

  2. 指定刷新间隔(5秒)

    1
    2
    3
    4
    5
    watch -n 5 free -h
    Every 5.0s: free -h hostname: Wed Jun 12 15:30:05 2024
    total used free shared buff/cache available
    Mem: 7.7G 2.1G 3.2G 200M 2.4G 5.2G
    Swap: 2.0G 0B 2.0G

    每5秒刷新一次内存使用情况。

  3. 高亮显示输出变化

    1
    2
    3
    4
    watch -d ls -l
    Every 2.0s: ls -l hostname: Wed Jun 12 15:30:10 2024
    total 8
    -rw-r--r-- 1 user user 0 Jun 12 15:30 new_file.txt

    当目录内文件变化时,变化的行会高亮显示。

  4. 当输出变化时自动退出

1
2
watch -g "ls | grep 'error.log'"
(当目录中出现 error.log 文件时,watch 自动退出)

监控目录,一旦出现 error.log 文件即停止监控。

  1. 隐藏顶部状态栏

    1
    2
    watch -t "uptime"
    15:30:15 up 1 day, 3:22, 2 users, load average: 0.08, 0.03, 0.01

    隐藏时间、命令和主机名等头部信息。

  2. 精确到秒的小数间隔

1
2
3
watch -n 0.5 "ps aux | grep python"
Every 0.5s: ps aux | grep python hostname: Wed Jun 12 15:30:20 2024
user 1234 0.0 0.1 12345 6789 pts/0 S+ 15:30 0:00 python script.py

每0.5秒刷新一次,监控 Python 进程。

  1. 结合管道和复杂命令

    1
    2
    3
    4
    watch "echo '当前连接数:' && netstat -an | grep ':80 ' | wc -l"
    Every 2.0s: echo '当前连接数:' && netstat -an | grep ':80 ' | wc -l
    当前连接数:
    12

    每2秒统计一次80端口的连接数。

  2. 执行命令后发出蜂鸣声

    1
    2
    3
    watch -b "ls /tmp/lockfile"
    Every 2.0s: ls /tmp/lockfile hostname: Wed Jun 12 15:30:25 2024
    ls: cannot access '/tmp/lockfile': No such file or directory

    当命令执行失败(如文件不存在)时,终端发出蜂鸣声。

常用参数选项

  • -n <秒>, –interval <秒>
    设置刷新间隔时间(默认2秒),支持小数(如 -n 0.5)。
  • -d, –differences
    高亮显示本次输出与上一次的变化部分。
  • –color
    与 -d 结合使用时,使用颜色高亮变化(需终端支持)。
  • -g, –chgexit
    当命令输出发生变化时,watch 自动退出。
  • -t, –no-title
    隐藏顶部状态栏(时间、命令和主机名)。
  • -b, –beep
    当命令的退出状态码非零时,发出蜂鸣声。
  • -e, –errexit
    命令执行错误(非零退出码)时停止刷新并退出。
  • -p, –precise
    精确计时模式,尝试抵消执行命令本身的时间消耗。

原厂文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
NAME

watch - execute a program periodically, showing output fullscreen

SYNOPSIS

watch [option ...] command

DESCRIPTION

watch runs command repeatedly, displaying its output and errors
(the first screenful). This allows you to watch the program output
change over time. By default, command is run every 2 seconds and
watch will run until interrupted. A header informs of the start
and running time of command as well as its exit code.

OPTIONS

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

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

-C, --no-color
Do not interpret ANSI color and style sequences.

-d, --differences[=permanent]
Highlight the differences between successive updates. If
the optional permanent argument is specified then watch
will show all changes since the first iteration.

-e, --errexit
Freeze updates on command error, and exit after a key
press. The exit code of watch will be the code command
exits with. If signal n is the cause of command
termination, the exit code will be 128 + n.

-g, --chgexit
Exit when the visible output of command changes. Changes
that are off the screen due to small screen size or large
output will not cause watch to exit.

-n, --interval seconds
Specify update interval. Values smaller than 0.1 and larger
than 2678400 (31 days) are converted into these respective
bounds. Both '.' and ',' work for any locale. The
WATCH_INTERVAL environment variable can be used to
persistently set a non-default interval (following the same
rules and formatting).

-p, --precise
Execute command --interval seconds after its previous run
started, instead of --interval seconds after its previous
run finished. If it's taking longer than --interval seconds
for command to complete, it is waited for in either case.

-q, --equexit <cycles>
Exit when output of command does not change for the given
number of cycles.

-r, --no-rerun
Do not run the program on terminal resize, the output of
the program will re-appear at the next regular run time.

-s, --shotsdir
Directory to save screenshots into.

-t, --no-title
Turn off the header normally shown at the top of the
screen.

-w, --no-wrap
Turn off line wrapping. Long lines will be truncated
instead of wrapped to the next line.

-x, --exec
Pass command to an exec(3) call instead of sh -c. The
program will start a bit quicker. Shell features
(environment setup, variable and pathname expansion, etc.)
will be unavailable.

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

-v, --version
Display version information and exit.

KEY CONTROL

spacebar
Issue command immediately. If it's running at the moment,
it is not interrupted and its next round will start without
delay.

q Quit watch. It currently does not interrupt a running
command (as opposed to terminating signals, such as the
SIGKILL following Ctrl+C).

s Take a screenshot. It will be saved in the working
directory, unless specified otherwise by --shotsdir. If
command is running at the moment, the screenshot will be
taken as soon as it finishes.

EXIT STATUS

0 Success. Does not represent command exit code.

1 Errors unrelated to command operation.

2 Errors related to command execution and management (not its
exit code).

any non-zero (--errexit)
With --errexit the last exit code of command is returned.

ENVIRONMENT

The behavior of watch is affected by the following environment
variables.

WATCH_INTERVAL
Update interval, follows the same rules as the --interval
command line option.

COLUMNS
Terminal screen character width. Set to override
autodetection.

LINES Terminal screen character height. Set to override
autodetection.

NOTES

POSIX option processing is used (i.e., option processing stops at
the first non-option argument). This means that flags after
command don't get interpreted by watch itself.

Non-printing characters are stripped from program output. Use cat
-v as part of the command pipeline if you want to see them.

EXAMPLES

To watch the contents of a directory change, you could use

watch -d ls -l

If you have CPUs with a dynamic frequency and want to observe it
change, try the following. The command is passed to the shell,
which allows you to make the pipeline. The quotes are a feature of
the shell too.

watch -n1 'grep "^cpu MHz" /proc/cpuinfo | sort -nrk4'

To monitor the up status of your servers, saving a copy of the
output of each run to a file, you may use this. The -p makes the
command execute every 10 seconds regardless of how long it took to
complete the previous run.

watch -n10 -p -d '{ date; for i in 10.0.0.31 10.0.0.32
10.0.0.33; do R=OK; ping -c2 -W2 "$i" &>/dev/null ||
R=FAIL; echo "$i: $R"; done } | tee -a ~/log'

You can watch for your administrator to install the latest kernel
with

watch uname -r

BUGS

When the terminal dimensions change, its contents changes are not
registered on the next command run. --chgexit will not trigger
that turn and the counter of --equexit will not restart even if
command output changes meanwhile. --differences highlighting is
reset.

REPORTING BUGS

Please send bug reports to ⟨procps@freelists.org⟩.

COLOPHON

This page is part of the procps-ng (/proc filesystem utilities)
project. Information about the project can be found at
⟨https://gitlab.com/procps-ng/procps⟩. If you have a bug report
for this manual page, see
⟨https://gitlab.com/procps-ng/procps/blob/master/Documentation/bugs.md⟩.
This page was obtained from the project's upstream Git repository
⟨https://gitlab.com/procps-ng/procps.git⟩ on 2024-02-02. (At that
time, the date of the most recent commit that was found in the
repository was 2024-01-15.) If you discover any rendering
problems in this HTML version of the page, or you believe there is
a better or more up-to-date source for the page, or you have
corrections or improvements to the information in this COLOPHON
(which is not part of the original manual page), send a mail to
man-pages@man7.org