On 8/23/2013 6:59 AM, Jake Vath wrote::

Thanks to all who replied to my cry for help.  I ended up changing the 
game plan a little, and it works fine now.

I changed the inotifywait to only monitor create & modify, and then 
append the file names to a log file (more often than not multiple 
instances of the same file were being recorded).

I then created a php script to parse the log file, just pull the unique 
names from the log, email the user the unique name, and wipe to log 
file.  I set this up as a cron job that runs every 10 minutes.

In the end, all is working as planned, just a couple extra steps involved.



> Ha, I just noticed that I used the wrong email in my script.
> So, if anyone is wondering with the email address changed... it was user
> error.
> Sorry about that.
>
> -> Jake
>
>
> On Fri, Aug 23, 2013 at 6:57 AM, Jake Vath <jake.vath at gmail.com
> <mailto:jake.vath at gmail.com>> wrote:
>
>     I modified the script a bit.
>     I added /set -e/ to the script and removed the pipe to /sendmail
>     /
>     All of the output is being directed to /stdout/.
>
>
>     #!/bin/bash
>     #
>     # usage: script DIR email-to-addr
>     set -e
>
>     DIR=$1
>     EMAILTO=$2
>
>     /usr/bin/inotifywait --recursive \
>
>                                 --monitor \
>                                 --quiet \
>                                 --exclude '.*\.tmp' \
>                                 --event close_write \
>                                 --format '%f' \
>                                 /home/jake/tmp/$DIR | while read FILE;
>
>     do
>     {
>              echo "To: $EMAILTO"
>              echo "From: MONITOR ROBOT <DO-NOT-REPLY at somewhere.com
>     <mailto:DO-NOT-REPLY at somewhere.com>>"
>              echo "Subject: Alert - $DIR"
>              echo " "
>              echo "A new file has been detected in $DIR"
>              echo ""
>              echo "The New File is named:"
>              echo " "
>              echo $FILE
>     } 2>&1
>     done
>
>     I executed the script like this:
>          jake at server:~$ ./mailme.sh dirToWatch jake.vath at gmail.com
>     <mailto:jake.vath at gmail.com>
>
>     Only get one "email" per file that I created.
>     Granted the email is not sent through /sendmail/.
>
>     After running this:
>         jake at server:~$ touch tmp ./dirToWatch/tmp
>
>     I get a "email" like this in my shell:
>     /To: jvath at erdc.k12.mn.us <mailto:jvath at erdc.k12.mn.us>/
>     /From: MONITOR ROBOT <DO-NOT-REPLY at somewhere.com
>     <mailto:DO-NOT-REPLY at somewhere.com>>/
>     /Subject: Alert - dirToWatch/
>
>     /A new file has been detected in dirToWatch/
>
>
>     /The New File is named:/
>
>     /tmp/
>
>     Do you think that it would be something with your mail server or
>     /sendmail/?
>
>     Mike, when I created some test files $FILE always changed.
>
>     Actually, I'm a little confused by the /2>&1 | /usr/bin/sendmail -t/
>     at the end of your do-while loop.
>     I know that /2>&1/ is going to redirect /stderr/ to /stdout/, but
>     that redirection only takes place IFF there /is/ and error, correct?
>     If that is correct and the normal output is going through the pipe
>     to /sendmail/ just like it looks like.
>
>     On a slightly related note, I read the man page for /sendmail/ and
>     came across this:
>
>         /Notes/
>
>         /sendmail often gets blamed for many problems that are actually
>         the result of other problems, such as overly permissive modes on
>         directories. For this reason, sendmail checks the modes on
>         system directories and files to determine if they can be
>         trusted. Although these checks can be turned off and your system
>         security reduced by setting the DontBlameSendmail option, the
>         permission problems should be fixed. For more information, see:/
>
>         /http://www.sendmail.org/tips/DontBlameSendmail.html/
>
>     I thought it was comical, as I'm sure they do get a lot bug reports
>     that are not /really/ bugs in /sendmail/.
>
>     -> Jake
>
>
>     On Fri, Aug 23, 2013 at 3:25 AM, Mike Miller <mbmiller+l at gmail.com
>     <mailto:mbmiller+l at gmail.com>> wrote:
>
>         Are the multiple email messages identical, or does $FILE change?
>
>         Mike
>
>
>
>         On Thu, 22 Aug 2013, Rick Engebretson wrote:
>
>             I'm not sure either. But it seems the pipe to while isn't a
>             pipe from one executable command output to another
>             executable command input.
>
>
>             Mike Miller wrote:
>
>                 Isn't the question here why it would send multiple
>                 messages when the
>                 event is close_write?
>
>                 I'm not clear on how "while read FILE" works, but that
>                 is the part that
>                 makes me suspicious.
>
>                 Mike
>
>
>                 On Wed, 21 Aug 2013, Jake Vath wrote:
>
>                     Do you want to stick with Bash for the solution?
>                     I've done something similar using Perl, so I bet I
>                     could modify it to do
>                     something like this.
>                     The Perl script uses *Inotify2*, so it's fairly
>                     portable.
>
>                     If you want to stick with Bash, maybe you could
>                     assemble your email
>                     into a
>                     few different strings, such as to, from, subject,
>                     and body.
>                     You could only send an email with all the previous
>                     information and the
>                     body
>                     of the emails concatenated together.
>                     That way you could build the emails based on some
>                     events and then send
>                     one
>                     email on a specific event.
>                     Think of it as a sentinel-controlled event loop.
>
>                     -> Jake
>
>
>                     On Tue, Aug 20, 2013 at 11:57 PM, B-o-B De Mars
>                     <mr.chew.baka at gmail.com
>                     <mailto:mr.chew.baka at gmail.com>>wrote:
>
>                         I need to monitor various directories contained
>                         in one base directory, and notify certain users
>                         by email when a file has been added or changed
>                         in their monitored directory. I wrote a script
>                         using inotifywait, and when an event is
>                         triggered it fires of an email to the user with
>                         the location & the new file name.
>
>                         The script is working, but can generate many
>                         emails for one event (saving a large file for
>                         example).
>
>                         I have tried many of the different --event types
>                         available in inotifywait to see if I could get
>                         it down to one notification. No luck yet. Here
>                         is the basic outline of the script. Any thoughts
>                         on how I might be able to get this to only send
>                         one email per file would be greatly appreciated.
>
>                         #!/bin/bash
>                         #
>                         # usage: script DIR email-to-addr
>
>                         DIR=$1
>                         EMAILTO=$2
>
>                         inotifywait --recursive --monitor --quiet
>                         --exclude '.*\.tmp' \
>                         --event close_write --format '%f' \
>                         /var/www/htdocs/contracts/**__contracts/$DIR |
>                         while read FILE ;
>                         do
>                         {
>                         echo "To: $EMAILTO"
>                         echo "From: MONITOR ROBOT
>                         <DO-NOT-REPLY at somewhere.com
>                         <mailto:DO-NOT-REPLY at somewhere.com>>"
>                         echo "Subject: Alert - $DIR"
>                         echo " "
>                         echo "A new file has been detected in $DIR"
>                         echo ""
>                         echo "The New File is named:"
>                         echo " "
>                         echo $FILE
>                         } 2>&1 | /usr/bin/sendmail -t
>                         done
>
>                         Thanks!
>
>                         Mr. B-o-B