Register
It is currently Wed Sep 03, 2014 1:00 am

monitor a directory size


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Fri Nov 23, 2007 8:21 am   

Joined: Fri Nov 23, 2007 8:14 am
Posts: 1
Hello!

I'm new to bash. I need a script that will compute the size of a directory and if it's > 9Mb it will send an email to a specific address.

I thought to use du -k but I need to keep the result of this command in a variable just to be able to compare later with 9000 in an if structure.
Do you know how can I do this or can you provide me another way of doing this script?

Any idea will be highly appreciated.

thank you very much
With best regards,
Sorin


Top
 Profile  
 PostPosted: Sun Dec 02, 2007 4:30 am   

Joined: Tue Dec 05, 2006 8:37 pm
Posts: 30
Location: Thiruvananthapuram / India
Hi Sorin,

The following script should help:

Code:
#!/bin/bash

# directory to be monitored
dir=/tmp

# limit on the size (in KB) of the directory
limit=9000

# alert email
email=you@domain.com

# if directory exists, find out it's size
if [ -d $dir ]
then
  size=$(du -sk $dir | cut -f1)
else
  echo "$dir is invalid !!!" | mail -s "Invalid directory" $email
  exit
fi

# if directory size is greater than limit, alert the user
if [ $size -gt $limit ]
then
  echo "Size of $dir is ${size}KB !" | mail -s "Directory size exceeded" $email
fi


Please put correct values for dir, limit and email. You can put this script as a cron job. For instance, if the path to the script is /home/sorin/monitor-dir.sh and you want to run the script every 15 minutes, then the cron job will be as follows:

*/15 * * * * /home/sorin/monitor-dir.sh

BTW make sure you make the script executable with the following command:

chmod 755 /home/sorin/monitor-dir.sh


Regards,
Lijeesh


Top
 Profile WWW YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP