LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Script to check file size (https://www.linuxquestions.org/questions/linux-general-1/script-to-check-file-size-4175537909/)

carancanfunfa 03-26-2015 09:23 AM

Script to check file size
 
Hello, we have a server in which a certain file keeps growing in size until it makes the server to crash down.
I want to know if it is possible to make a script that constantly checks the file size and send some kind of alert once it gets a certain size. The alert could be a banner at login or an email sent to some administrator.
Do you think it is possible?

TB0ne 03-26-2015 09:33 AM

Quote:

Originally Posted by carancanfunfa (Post 5337896)
Hello, we have a server in which a certain file keeps growing in size until it makes the server to crash down.
I want to know if it is possible to make a script that constantly checks the file size and send some kind of alert once it gets a certain size. The alert could be a banner at login or an email sent to some administrator.
Do you think it is possible?

Very possible, and there are many tutorials and samples to watch files already, which you can find with a Google search. What are you having trouble with? We'll be happy to help, but you have to post a specific question.

pan64 03-26-2015 09:50 AM

sure, it is possible, probably you need to configure your cron. But probably you want to check the utility logrotate.

rtmistler 03-26-2015 12:12 PM

I second the recommendation for logrotate. Doesn't matter where the file is located or what type of file it is, logrotate can check it and archive it or remove it depending on just the size of the file. You can also invoke logrotate faster than once per day if the growth of this file is too rapid.

eklavya 03-30-2015 09:02 AM

Code:

#!/bin/bash
var=$(ls -l /path/of/filename.extension | awk '{print $4}')
# or
var=$(find /path/of/the/directory  -name "filename.extension" -ls | awk '{total += $7} END {print total}')
if [ $var -gt 250000000 ] ## suppose your limit is 250 MB
then
echo "mailbody" | mail -s "filesize limit exceeds" username@mail.com
fi



All times are GMT -5. The time now is 03:15 PM.