我有这个:
#!/bin/bash
while :
do
SUM=$(tree | md5sum)
if [ "$SUMOLD" != "$SUM" ]; then
# do something here
SUMOLD=$SUM
sleep 1
fi
done
Run Code Online (Sandbox Code Playgroud)
哪个工作得很好.但是,问题在于它消耗了50%的CPU,即Core2 Duo T8300.这是为什么?如何提高效率?
这是一份工作inotifywait.inotify是Linux基于事件的系统,用于监视文件和目录以进行更改.再见投票循环!
NAME
inotifywait- 等待使用inotify更改文件概要
Run Code Online (Sandbox Code Playgroud)inotifywait [-hcmrq] [-e <event> ] [-t <seconds> ] [--format <fmt> ] [--timefmt <fmt> ] <file> [ ... ]说明
inotifywait使用Linux的inotify(7)界面有效地等待文件的更改.它适用于等待shell脚本中的文件更改.它可以在事件发生时退出,也可以在事件发生时继续执行和输出事件.
以下是编写一个简单循环的方法,该循环可以检测从目录添加,修改或删除文件的时间:
inotifywait -mq /dir | while read event; do
echo "something happened in /dir: $event"
done
Run Code Online (Sandbox Code Playgroud)
查看手册页以获取更多选项.如果您只关心修改并希望忽略只是被读取的文件,您可以使用-e来限制事件的类型.
| 归档时间: |
|
| 查看次数: |
47 次 |
| 最近记录: |