0

Or: How do I prevent a sudo'ed rsync from infinite firing in a while-loop? Because that's both what (feels like) is happening and I don't get it.

I am trying to set up a watch for syncing modified files, and it works fine. However, once I introduce the required sudo to the rsync command, a single inotify event causes the rsync command to fire indefinitely.

#!/usr/bin/env bash
inotifywait -m -r --format '%w%f' -e modify -e move -e create -e delete /var/test | while read line; do
    sudo rsync -ah --del --progress --stats --update "$line" "/home/test/" 
done

When you edit a file, rsync goes in rapid fire mode. But lose the sudo (and use folders to which you have permissions, of course) and the script works as expected.

  1. Why is this?
  2. How do I make this work correctly with the sudo command?
4

1 回答 1

1

我有答案,通过实验找到的。但我不知道为什么会这样。请有人告诉我为什么sudo在这个循环中破坏了预期的阻塞行为。

由于sudo破坏了脚本,我们可以sudo通过使用包装器来远离:
这是正确的:

inotifywait -m -r --format '%w%f' -e modify /var/test | while read line; do
    sh -c 'sudo rsync -ah "$line" "/home/test/"'
done

奇怪的是:sudo从包装中取出,我们又遇到了旧的错误行为。很奇怪。
这是错误的:

inotifywait -m -r --format '%w%f' -e modify /var/test | while read line; do
    sudo sh -c 'rsync -ah "$line" "/home/test/"'
done
于 2012-12-29T16:43:52.870 回答