2019-04-21 11:50:48 +08:00

29 lines
4.3 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

<h1 id="watch">watch</h1>
<p>可以将命令的输出结果输出到标准输出设备,多用于周期性执行命令/定时执行命令</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>watch命令</strong> 以周期性的方式执行给定的指令指令输出以全屏方式显示。watch是一个非常实用的命令基本所有的Linux发行版都带有这个小工具如同名字一样watch可以帮你监测一个命令的运行结果省得你一遍遍的手动运行。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">watch(选项)(参数)</code></pre>
<h3 id="选项">选项</h3>
<div class="sourceCode" id="cb2"><pre><code class="language-bash"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="ex">-n</span> <span class="co"># 或--interval watch缺省每2秒运行一下程序可以用-n或-interval来指定间隔的时间。</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2"><span class="ex">-d</span> <span class="co"># 或--differences 用-d或--differences 选项watch 会高亮显示变化的区域。 而-d=cumulative选项会把变动过的地方(不管最近的那次有没有变动)都高亮显示出来。</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3"><span class="ex">-t</span> <span class="co"># 或-no-title 会关闭watch命令在顶部的时间间隔,命令,当前时间的输出。</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4"><span class="ex">-h</span>, --help <span class="co"># 查看帮助文档</span></a></code></pre></div>
<h3 id="参数">参数</h3>
<p>指令:需要周期性执行的指令。</p>
<h3 id="实例">实例</h3>
<div class="sourceCode" id="cb3"><pre><code class="language-bash"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="ex">watch</span> -n 1 -d netstat -ant       <span class="co"># 命令:每隔一秒高亮显示网络链接数的变化情况</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="ex">watch</span> -n 1 -d <span class="st">&#39;pstree|grep http&#39;</span> <span class="co"># 每隔一秒高亮显示http链接数的变化情况。 后面接的命令若带有管道符,需要加&#39;&#39;将命令区域归整。</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3"><span class="ex">watch</span> <span class="st">&#39;netstat -an | grep:21 | \ grep&lt;模拟攻击客户机的IP&gt;| wc -l&#39;</span> <span class="co"># 实时查看模拟攻击客户机建立起来的连接数</span></a>
<a class="sourceLine" id="cb3-4" data-line-number="4"><span class="ex">watch</span> -d <span class="st">&#39;ls -l|grep scf&#39;</span> # 监测当前目录中 scf<span class="st">&#39; 的文件的变化</span></a>
<a class="sourceLine" id="cb3-5" data-line-number="5"><span class="st">watch -n 10 &#39;</span>cat /proc/loadavg<span class="st">&#39; # 10秒一次输出系统的平均负载</span></a>
<a class="sourceLine" id="cb3-6" data-line-number="6"><span class="st">watch uptime</span></a>
<a class="sourceLine" id="cb3-7" data-line-number="7"><span class="st">watch -t uptime</span></a>
<a class="sourceLine" id="cb3-8" data-line-number="8"><span class="st">watch -d -n 1 netstat -ntlp</span></a>
<a class="sourceLine" id="cb3-9" data-line-number="9"><span class="st">watch -d &#39;</span>ls -l <span class="kw">|</span> <span class="fu">fgrep</span> goface<span class="st">&#39; # 监测goface的文件</span></a>
<a class="sourceLine" id="cb3-10" data-line-number="10"><span class="st">watch -t -differences=cumulative uptime</span></a>
<a class="sourceLine" id="cb3-11" data-line-number="11"><span class="st">watch -n 60 from # 监控mail</span></a>
<a class="sourceLine" id="cb3-12" data-line-number="12"><span class="st">watch -n 1 &quot;df -i;df&quot; # 监测磁盘inode和block数目变化情况</span></a></code></pre></div>
<p>FreeBSD和Linux下watch命令的不同在Linux下watch是周期性的执行下个程序并全屏显示执行结果<code>watch -n 1 -d netstat -ant</code>而在FreeBSD下的watch命令是查看其它用户的正在运行的操作watch允许你偷看其它terminal正在做什么该命令只能让超级用户使用。</p>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->