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?
Redsandro
  • 11,060
  • 13
  • 76
  • 106
  • When you apply sudo, I suspect rsync is waiting for password to be entered. and that's how you think rsync is running indefinitely. Have you tried, running sudo'ed rsync on command prompt as a standalone command ? does it ask for password ? if yes, you might want to use expect command to automate the password entry. – Icarus3 Dec 28 '12 at 12:09
  • The command works fine without a password - I have `rsync` allowed in `sudoers` with `nopassword`. The command also executes properly, it syncs the file. But it keeps doing it over and over until I break the script. – Redsandro Dec 28 '12 at 12:45
  • Ahh !! got your problem !! the thing is when you edit any file, most of the editor create swap file or even update the timestamp. this issues many modify notifications to "inotifywait" you should be passing "-e close_write" switch to inotifywait command. in fact, just don't look for modifications. see, http://stackoverflow.com/questions/10300835/too-many-inotify-events-while-editing-in-vim – Icarus3 Dec 28 '12 at 13:09
  • Unfortunately, this is unrelated. Otherwise this behavior would also affect the non-sudo variant. Besides, I also want to monitor creations, deletions and moves. I am testing using `echo test > test.txt`, no temp files or attribute changes here. – Redsandro Dec 28 '12 at 13:16
  • thats correct, echo would not do that. try "vim /var/test/a.txt" and keep editing it while your script is running. Whenever you press enter in vim, you will see rsync running. no ? – Icarus3 Dec 28 '12 at 13:17
  • I don't see a condition in your while loop that says, "ok, enough, lets quit". Isn't `inotifywait` meant to keep running forever? Good luck. – shellter Dec 28 '12 at 22:33
  • **@shellter:** It's supposed to run indefinitely, and the original script works fine. The introduction of `sudo` breaks this behavior, and that is the scope of this question. **@Icarus3:** Probably. I believe you. :) I haven't checked, this is irrelevant. **@All:** I answered my own question, but I don't understand why `sudo` breaks the expected bash behavior. – Redsandro Dec 29 '12 at 16:52
  • your heading says "how do I prevent a sudo'ed rsync from infinite firing in a while-loop?", but NOT "why is my infinite loop not working any more, after I have added sudo", which is what your comment indicates is the problem. Glad you found a solution to your problem and that you posted the answer here. Don't forget to accept your answer with a chk-mark to increase your reputation points!. Good luck. – shellter Jan 01 '13 at 23:31

1 Answers1

1

I have the answer, found it by experimenting. But I have no idea why this is. Please someone tell me why sudo in this loop breaks the expected blocking behavior.

Since sudo breaks the script, we can distance ourselves from sudo by using a wrapper:
This is correct:

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

Weird thing is: Pull the sudo out of the wrapper and we have the old faulty behavior again. Very strange.
This is wrong:

inotifywait -m -r --format '%w%f' -e modify /var/test | while read line; do
    sudo sh -c 'rsync -ah "$line" "/home/test/"'
done
Redsandro
  • 11,060
  • 13
  • 76
  • 106