手表成立后,为什么我的Inotify脚本不会更进一步?

时间:2018-05-04 06:20:14

标签: linux bash

我在本教程https://linuxhint.com/inotofy-rsync-bash-live-backups/之后制作了一个脚本,用于与rsync合并inotify 但我面临一个问题。花了这么多时间,在显示这个输出后不会再进一步​​了

Setting up watches.
Watches established.

这是我的剧本

!/bin/bash
while inotifywait -e modify,create,delete /home/My_home/ITB/uploads
do 
    rsync -avz /home/My_home/ITB/uploads /home/My_home/destination
done

1 个答案:

答案 0 :(得分:1)

您的rsync循环向后,您只想在inotify触发时触发inotifywait,以响应目录中的文件更改 - 不重复调用{{1} }。您还应为-me添加-minotifywait监视器)选项,例如

inotifywait -me modify,create,delete /home/Mansoor/ITB/uploads | while read; do
    rsync -avz /home/My_home/ITB/uploads /home/My_home/destination
done

这样,每次inotifywait触发(您不捕获输出),只需调用rsync即可同步目录。

以下是一个例子:

示例脚本

#!/bin/bash

inotifywait -me modify,create,delete ~/dev/src-c/tmp/debug/dir1 | while read; do
    rsync -avz ~/dev/src-c/tmp/debug/dir1/ ~/dev/src-c/tmp/debug/dir2
done

创建2个空目录

$ ls -al dir1
total 52
drwxr-xr-x  2 david david  4096 May  4 01:42 .
drwxr-xr-x 14 david david 49152 May  4 01:38 ..

$ ls -al dir2
total 52
drwxr-xr-x  2 david david  4096 May  4 01:42 .
drwxr-xr-x 14 david david 49152 May  4 01:38 ..

启动inotifywait脚本

$ bash inw.sh &
Setting up watches.
Watches established.

进行更改

$ touch dir1/foo

(由rsync触发的inotifywait产生的额外终端输出)

sending incremental file list
./
foo

sent 115 bytes  received 38 bytes  306.00 bytes/sec
total size is 0  speedup is 0.00

验证其有效

$ ls -al dir2
total 52
drwxr-xr-x  2 david david  4096 May  4 01:44 .
drwxr-xr-x 14 david david 49152 May  4 01:38 ..
-rw-r--r--  1 david david     0 May  4 01:44 foo

是的,像宣传的那样工作......

再做几次

$ touch dir1/bar
$ touch dir1/baz

并检查

$ ls -al dir2
drwxr-xr-x  2 david david  4096 May  4 01:54 .
drwxr-xr-x 14 david david 49152 May  4 01:38 ..
-rw-r--r--  1 david david     0 May  4 01:53 bar
-rw-r--r--  1 david david     0 May  4 01:54 baz
-rw-r--r--  1 david david     0 May  4 01:44 foo