I have a small script I wrote to look for empty files on a Linux box. It finds the empty files and emails the list to me. That works great. What I need to do now is set a cron job to run the thing every 10 minutes or so (no problems there) but I don't want to get any more emails if the last email has the same contents as the last time the script ran. Does that make sense? I only want to get new email if there is a change in the number or names of the empty files it finds.
Thank you for any input. I stink at scripting of any kind.
#!/bin/bash
FQDN=`/bin/domainname -f`
FILE=/tmp/$FQDN.`date +%F`.txt
exec &> $FILE
dir=/home/
COUNT=`find $dir -type f -empty | wc -l`
if [ ! -f $FILE ]; then
echo " "
echo "The following files are empty... "
echo " "
find $dir -type f -empty
echo " "
mail -s "I Have $COUNT Empty Files" wviands@my.email < $FILE
fi
Considering count will not help, please consider if count is same but file(name)s are different.
you could try like this:
Redirect the list of empty files to a file (better to sort ) and save it. In the next run, generate a new list and do a diff command with previous one and based on diff command exit status is not zero (0 - inputs are same , 1 - inputs are different ) then you can copy the new file to old and trigger a mail.
Do you also want a mail if zero length files have disappeared? If not, you could send a mail only if new files have appeared ever since the last time your cron script ran.
Just looking for new files. Eventually the zero byte files will be deleted but I expect new ones to show up occasionally.
I'm still fiddling with the diff stuff.
I was spoiled earlier in life and always had a code monkey I could pester to do this stuff but now they're all gone and I'm paying for it. Working in bash from the command line is a no brainer for me but putting it into a script, well, I sometimes think my mind doesn't work that way. Might be just a lack of practice though.
You could store the output which has been mailed once in a certain file, and always compare the contents of the new output (which would be mailed) and the old one (using a tool such as cmp), and only send the mail if they are different.
Don't do the exec stuff. Separate your task into two scripts.
One is calculating the message to be sent by email, but it does not actually send the mail, but writing the information to be sent to stdout.
The other script - the master script - is calling the calculating script and redirects its output to $FILE. Then it compares the ouput to saved.txt, and if they are different, it sends the mail and copies $FILE to saved.txt.
The script I posted above is definitely working. I'm running it from cron every minute and tossing in or deleting random numbers of 0 byte files.
I tried...
find $dir -type f -empty > $FILE 2>&1
[[ -f saved.txt ]] || touch saved.txt
if ! cmp -s saved.txt $FILE ; then
mail -s "HFS Test Has $COUNT Empty Files" wviands@email < $FILE
fi
...but it didn't seem to work, which means I didn't do it right. I don't write scripts but I know I need to learn. What is it that I did wrong? I'm probably not 'reading' what the script is doing correctly. Most likely I think it's doing one thing but it's really doing something else entirely.
I appreciate your taking the time to show me better ways of doing things.
PLEASE provide more info: WHAT "didn't seem to work"? Post the contents of the files, the values of the variables, the result of the cmp command, mayhap a log of the script's execution.
Basically, I made a complete mess of your script. Good grief, even my reading skills have gone downhill. My apologies, it's working fine now that I put in what you posted instead of what I thought I saw.
Maybe you were just too quick when looking. After I posted, I immediately noticed an error in my script and edited again. This took perhaps one minute. If you have managed to read my post during this time window, you would indeed have got a non-working version.
Corollary: Never blindly copy code, which someone posted. Always try to understand it first. People do make mistakes.