Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I'm trying to make this bash script dispatch and email when it detects that a certain type of file (files ending in ".gz"), which was last modified 24 hours ago, is below a certain size in a directory, I currently have it running on my entire computer. (I have it set to 10 kilobytes).

It just sends an email regardless, I expect it only to send one if it actually detects that the file is under the threshold, and if someone could please go through it and try to tell me where I messed it up that would be great! This is one of my first serious scripts so go easy on the vocabulary!

1) The script will find all of the files in a directory ending with ".gz" that were modified in the last 24 hours.

2) It will check these files and make sure they are above a certain threshold in size (say 10 kilobytes for example)

3) If they are it will do nothing, but if they are under that threshold, an email will be dispatched to the said email address

#!/bin/bash
for file in /*; do
stat $file
    FAILURE= find . -name "*.gz" -size -10k -mtime -1 -printf 'Failure %p\n'
done
if $FAILURE
then
    echo "The backup test has failed!" | mail -s "BACKUP FAILURE" [email protected]
fi
share|improve this question
    
No need to reinvent the wheel here: Tools like Monit have been written for this exact purpose. –  n.st Jun 23 at 7:58

2 Answers 2

Here is your script modified slightly:

#!/bin/bash
if files=$(find . -name "*.gz" -size -10k -mtime -1 -print) ; then
   echo "The backup test has failed!\n$files" | mail -s "BACKUP FAILURE"   [email protected]
fi

You don't need to put this in a for loop; there is an implicit loop in the find comand.

One debugging technique you can use if you haven't figured this out already is to run parts of the commands above to test them out.

However there is also a bash debugger.

share|improve this answer

I assume you want to check for small gz files in this folder /usr/test, the follwoing should help you. it will send you a log file of all the gz files under this path and email you the list. You can use this list for later use.

My script would be:

#!/bin/bash
size_checker ()
{
cd /usr/test
ls *.gz > gzfiles
for FILE in `cat gzfiles`
 do
  SIZE=`ls -la $FILE | awk '{print $5}'`
   if [ "$SIZE" -lt 10241 ]
    then
     LAST_MODIFIED=`ls -la $FILE | awk '{print $6,$7}'` 
    echo "${FILE} ${LAST_MODIFIED}" >> log
   if
done   
uuencode log log.txt | mailx -s "Log of small gz files" [email protected]
}
size_checker
exit 0

Modified time has been added, you will receive all the the gz files less than or equal to 10kb with their last modified date .

share|improve this answer
    
The purpose of the script would be to check a backup and ensure it is above a certain threshold. The script will run once a day and if the backup were to fail (the files of this type is are small) it would send an email. –  Aiden Prohaska Jun 23 at 1:32
    
The part where it runs once a day won't be part of the script. –  Aiden Prohaska Jun 23 at 1:55
    
@AidenProhaska can you please verify what exactly you want. Please explain your original scenario properly. To make the script run once per day usually at nights, edit crontab and add your script to crontab to make it run once a day. –  ayrton_senna Jun 23 at 2:26
    
I've added a few bullet points to the body of the question. –  Aiden Prohaska Jun 23 at 2:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.