watch周期性执行给定的指令

以将命令的输出结果输出到标准输出设备,多用于周期性执行命令/定时执行命令
watch命令 以周期性的方式执行给定的指令,指令输出以全屏方式显示。watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行。
命令格式:

watch [参数] [命令]

常用命令选项

Options:
  -b, --beep             beep if command has a non-zero exit
  -c, --color            解释ANSI颜色和样式序列
  -d, --differences[=<permanent>]	高亮显示变化的区域
                         突出显示更新之间的更改
  -e, --errexit          退出if命令有一个非零退出
  -g, --chgexit          当命令的输出发生变化时退出
  -n, --interval <secs>  更新之间要等待几秒
  -p, --precise          尝试在精确的间隔内运行命令
  -t, --no-title         关闭页眉
  -x, --exec             将命令传递给exec,而不是“sh -c”
 -h, --help     		 显示此帮助并退出
 -v, --version  		 output version information and exit

参考实例

watch -n 5 -d "uptime"或watch -n 5 'cat /proc/loadavg'  		# 每间隔 5 秒钟输出系统的平均负载
watch -n 1 -d netstat -ant       # 每隔一秒高亮显示网络链接数的变化情况
watch -n 1 -d 'pstree|grep http' # 每隔一秒高亮显示http链接数的变化情况。 后面接的命令若带有管道符,需要加''将命令区域归整。
watch 'netstat -an | grep:21 | \ grep<模拟攻击客户机的IP>| wc -l' # 实时查看模拟攻击客户机建立起来的连接数
watch -d 'ls -l|grep scf'        # 监测当前目录中 scf' 的文件的变化
watch -d -n 1 netstat -ntlp
watch -d 'ls -l | fgrep goface'  # 监测goface的文件
watch -t -differences=cumulative uptime
watch -n 60 from            	 # 监控mail
watch -n 1 "df -i;df"       	 # 监测磁盘inode和block数目变化情况

FreeBSDLinux下watch命令的不同,在Linux下watch是周期性的执行下个程序,并全屏显示执行结果,如:watch -n 1 -d netstat -ant,而在FreeBSD下的watch命令是查看其它用户的正在运行的操作,watch允许你偷看其它terminal正在做什么,该命令只能让超级用户使用。
Laziness will delay a lot of things, action is the weapon to break it.

猜你喜欢

转载自blog.csdn.net/qq_50573146/article/details/125463515