Using inotifywait with queue directories

After writing Using directories as queues I realized I did not mention how you should check the queue. Using a cron job is usually adequate. However, this does mean your script will be polling the queue-directory and having to distinguish been an empty and a non-empty queue-directory. If you don't like polling then use inotify and specifically inotifywait (part of the Ubuntu package inotify-tools). For example, this command line will monitor the directory /queue/new and when files are added to or moved into the directory the word count command is run.

Q=/queue ; inotifywait \
   --monitor \
   --event moved_to \
   --event create \
   --format "mv $Q/new/%f $Q/cur/ && wc $Q/cur/%f" $Q/new/ | sh