Avoid multiple emails being sent - Counter, Loop?

Hello,

I have currently coded a bash script below in which it does the following:

# Archives compressed file from another location.

Basically it moves *.gz files to another location.

The script also sends an email whenever a new compressed file is placed. This is the issue that i have. Whenever a new file is placed, the mail is sent, but it continues to do so every 30 minutes (as set in cron).

The problem is i want to sent the email one time whenever the compressed file is sent, then of course another one once another file is placed (can be in same day).

Here's my code:

#!/bin/bash
# Archive compressed core files 
CORE_DIR="/var/crash"
ARCHIVE_DIR="/mnt/Backups/crash"
mv $CORE_DIR/*.gz $ARCHIVE_DIR
 
touch -t `date +%m%d0000` /tmp/$$
cd $ARCHIVE_DIR
zip=`find 2>/dev/null core*.gz -type f -newer /tmp/$$ | wc -l`
rm /tmp/$$
 
if [ $zip -ge 1 ] 
then
ls -ltr /mnt/Backups/crash | mailx -v -s "Crash File zipped successfully in /mnt /Backups/crash" -r user@xxxxxx.com  anotheruser@xxxxxx.com
 
fi
 

How can i achieve this. I was thinking about using loop or a counter, but if these are needed i would like to get some help from you, as i don't have any knowledge when it comes to counters or loops.

Can you give me some feedback ?

Rgds,

Matthew

Typical in an approach like this, is to move the file (the zip file and/or originating file) after it has been emailed. Put it into a different folder.
Thus, you would not find a file next time thru the loop.

Not sure I understand your logics. Do you want to send the mail whenever your script moved files over to $ARCHIVE_DIR? Would your script be the only mover? Then use the exit code of mv : If it's 0, file(s) were successfully moved, and you can send the mail. If your script is not the only mover, touch a time stamp file in $ARCHIVE_DIR in every run and use that for the find -newer command.

Hi Joeyg and RudiC thanks for sparing time to reply.

No i'm not emailing the file. The intention is to send the mail whenever the script moves file or files over to $ARCHIVE_DIR. So yes RudiC that's the idea.

So two things:

It seems the exit code of the

mv $CORE_DIR/*.gz $ARCHIVE_DIR

command might do the trick. How can this be done? Do i need to redirect the mv command?

If the exit code of the mv command is "0" then the mailx is triggered. So i am afraid there will be still multiple emails since if there are no files the exit code will still be 0 right?

No, exit code will be 1 if no files moved. Use the -v (verbose) option to mv to list the files actually moved. Try like:

mv -v $CORE_DIR/*.gz $ARCHIVE_DIR >/tmp/mv.log && mailx ... </tmp/mv.log

and adapt to your needs.

Hi again,

will try

echo $?

---------- Post updated at 02:32 PM ---------- Previous update was at 02:29 PM ----------

Thanks RudiC. Will try the verbose option. The check will made on the mv command, so i can also skip the find command in my script which will check for files with the -mtime paramter.

---------- Post updated at 03:14 PM ---------- Previous update was at 02:32 PM ----------

It worked by using the echo $? > mv.log. Then using head for the file followed by an if condition to match if equal to 0 (mv successful)