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>"
        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

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*
*From: MONITOR ROBOT <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> 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>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>"
>>>>> 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
>>>>> ______________________________****_________________
>>>>> TCLUG Mailing List - Minneapolis/St. Paul, Minnesota
>>>>> tclug-list at mn-linux.org
>>>>> http://mailman.mn-linux.org/****mailman/listinfo/tclug-list<http://mailman.mn-linux.org/**mailman/listinfo/tclug-list>
>>>>> <ht**tp://mailman.mn-linux.org/**mailman/listinfo/tclug-list<http://mailman.mn-linux.org/mailman/listinfo/tclug-list>
>>>>> >
>>>>>
>>>>>
>>>>>
>>>>  ______________________________**_________________
>>> TCLUG Mailing List - Minneapolis/St. Paul, Minnesota
>>> tclug-list at mn-linux.org
>>> http://mailman.mn-linux.org/**mailman/listinfo/tclug-list<http://mailman.mn-linux.org/mailman/listinfo/tclug-list>
>>>
>>>  ______________________________**_________________
>> TCLUG Mailing List - Minneapolis/St. Paul, Minnesota
>> tclug-list at mn-linux.org
>> http://mailman.mn-linux.org/**mailman/listinfo/tclug-list<http://mailman.mn-linux.org/mailman/listinfo/tclug-list>
>>
>>  ______________________________**_________________
> TCLUG Mailing List - Minneapolis/St. Paul, Minnesota
> tclug-list at mn-linux.org
> http://mailman.mn-linux.org/**mailman/listinfo/tclug-list<http://mailman.mn-linux.org/mailman/listinfo/tclug-list>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.mn-linux.org/pipermail/tclug-list/attachments/20130823/35e4e94a/attachment.html>