Avoid multiple emails being sent - Counter, Loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Avoid multiple emails being sent - Counter, Loop?
# 1  
Old 06-10-2014
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:

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
# 2  
Old 06-10-2014
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.
# 3  
Old 06-10-2014
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.
# 4  
Old 06-10-2014
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
Code:
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?
# 5  
Old 06-10-2014
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:
Code:
mv -v $CORE_DIR/*.gz $ARCHIVE_DIR >/tmp/mv.log && mailx ... </tmp/mv.log

and adapt to your needs.
# 6  
Old 06-10-2014
Hi again,

will try
Code:
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)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop with limit counter

#!/usr/bin/ksh c=0 while ]; do echo /tmp/unex NOT found, iter : $c; ((c = $c + 1)); sleep 2; done so, the above counter doesn't work, already tried both -lt & -gt, and changed || to && so what am I missing? Thanks in advance (5 Replies)
Discussion started by: unexistance
5 Replies

2. Shell Programming and Scripting

Loop counter resets by itself

Hi! Can anyone explain this? The counter CDR_count should go on forever, but it suddenly resets at every step of the FOR loop (I know this because $_file has exactly 378 records). However, the counter reset is OUTSIDE the FOR loop. What's going on?? #!/bin/bash if || ; then echo ... (8 Replies)
Discussion started by: Flavius
8 Replies

3. Shell Programming and Scripting

Loop usage with counter

Hi friends, I just want to try some thing with shell using loop and counter I have 30 Directory, in each directory number of files say 5, 10, 20 etc... directory_1 directory_2 directory_n what I want to do is read files from directory_1 say 5 files if my counter is like this m=2000 ... (6 Replies)
Discussion started by: Akshay Hegde
6 Replies

4. Homework & Coursework Questions

Min/Max/counter/while loop from file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The program is supposed to read in text from a given file ( different samples provided in the homework but not... (1 Reply)
Discussion started by: c++newb
1 Replies

5. Shell Programming and Scripting

How to implement the counter in loop?

Hi, I am working on a script where I need to add one functionality i.e. to could the number of tar files at particular location...but the script is working in below way. 1) create sandbox 2) Drop old member function 3) addmember function 4) Apply checkpoint lable 5) Resync operation(This... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

6. Shell Programming and Scripting

loop with a counter on a constant in bash

Hello Everyone, I'm in need of assistance on creating a script with a counter on a certain string. Basically this script opens a log file and displays certain log data. There are two key words in the log. START and FINISH. In between the START and FINISH is a variable ACTNUMBER. It will... (1 Reply)
Discussion started by: rxc23816
1 Replies

7. Shell Programming and Scripting

S# in a for loop - concatenate $(loop counter)

Hi, hope I am posting in the right section. My problem is that I have 2 or more arguments passed and I want to check if the arguments passed exists or not. The first argument should not exist and the remaining others should exist. example: ./shells.sh argument1 argument2 argument3 ... (5 Replies)
Discussion started by: fight4love
5 Replies

8. Shell Programming and Scripting

adding counter to a variable while moving in a loop

The scenario is like this : I need to read records from a file one by one and increment counter1, if a certain field matches with a number say "40"..the script should increment the counter2 and also extract a corresponding field from the same line and adding them one by one and redirecting the the... (5 Replies)
Discussion started by: mady135
5 Replies

9. Shell Programming and Scripting

How to avoid multiple while loop?

Hi, How can I avoid multiple 'cat while read ....? in my script. In my script, I am taking the inputs from the temp text file and doing the ( cat while read input do ... ... done ) task and deleting later. I know it'll raise the perfomance issue. How to avoid this? (2 Replies)
Discussion started by: sharif
2 Replies

10. Shell Programming and Scripting

Loop counter variable display

Hello everyone, how can I send output to the screen from a running script or tcl, in such a way that if a loop is executing I will see the rolling counter on my screen as the records are processed in the loop. I do not want the screen to scroll, though. In other words can a var's value be painted... (2 Replies)
Discussion started by: lifespan
2 Replies
Login or Register to Ask a Question