标签搜索

目 录CONTENT

文章目录

Linux shell 命令 watch 使用说明

沙漠渔
2022-06-07 18:38:15 / 0 评论 / 0 点赞 / 456 阅读 / 1,457 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-06-09,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

需要周期执行一个命令,不想手动一遍一遍的执行,就可以使用watch

6068133a15ac6ead4e47e24e2a826b88

命令简介

watch 命令周期性的方式执行给定的命令,并全屏显示执行结果。
watch 命令是一个非常实用的 shell 命令,基本上所有的 Linux 发行版都自带,watch可以帮助检测一个命令的运行结果,避免一遍一遍地手动执行检测命令。

命令格式

watch 命令的帮助信息如下:

~$ watch --help

用法:
 watch [options] command

选项:
  -b, --beep             beep if command has a non-zero exit
  -c, --color            interpret ANSI color and style sequences
  -d, --differences[=<permanent>]
                         highlight changes between updates
  -e, --errexit          exit if command has a non-zero exit
  -g, --chgexit          exit when output from command changes
  -n, --interval <secs>  seconds to wait between updates
  -p, --precise          attempt run command in precise intervals
  -t, --no-title         turn off header
  -x, --exec             pass command to exec instead of "sh -c"

 -h, --help     显示此帮助然后离开
 -v, --version  output version information and exit

常用的参数主要包括:

-n 或者 --intervalwatch 命令默认情况下每间隔 2 秒钟执行一次程序,可使用该参数指定间隔时间。

-d 或者 --differences:高亮显示变化的区域

-t 或者 --no-title:会关闭 watch 命令在顶部显示的时间间隔、命令、当前时间输出等头部信息

命令示例

每间隔 1 秒钟高亮显示网络链接数的变化情况:

watch -n 1 -d netstat -ant

每间隔 1 秒钟高亮显示 http 链接数的变化情况:

watch -n 1 -d 'pstree|grep http'

监控当前目录中 scf 文件的变化情况:

watch -d 'ls -l | grep scf'

每间隔 5 秒钟输出系统的平均负载

watch -n 5 -d "uptime"
0
广告 广告

评论区