aot2002
asked on
linux shell script
linux shell script to check directory diskspace if over 100mb it will email me !
i tried using the below php script yet i still cannot get the information into a variable
<?php
$tmp=passthru('du -hs /var/www/html/*');
//if($tmp > 100mb then email me something !
echo $tmp;
?>
i tried using the below php script yet i still cannot get the information into a variable
<?php
$tmp=passthru('du -hs /var/www/html/*');
//if($tmp > 100mb then email me something !
echo $tmp;
?>
Your* ... alittle typo there.
here you have the script but in bash:
#!/bin/bash
USAGE=`du -s /var/www/html/ | awk '{ print $1 }'`
if [ $USAGE -ge 100000 ]; then
(echo "To: (yourmail adress here)"; echo "Subject: 100 Mb or more on html drive"; echo "Dont drink and drive." ) | sendmail -t
fi
#End
maybe you have to put the entire path to sendmail if you run it as a crontab
#!/bin/bash
USAGE=`du -s /var/www/html/ | awk '{ print $1 }'`
if [ $USAGE -ge 100000 ]; then
(echo "To: (yourmail adress here)"; echo "Subject: 100 Mb or more on html drive"; echo "Dont drink and drive." ) | sendmail -t
fi
#End
maybe you have to put the entire path to sendmail if you run it as a crontab
except 100000 is not 100 MB it is 100000 Kilobytes and 1024 kilobytes is one megabyte therefore 102400 is the number you should be comparing against.
BTW php ---- do you want this to be a web script? If not go for the bash version, there is no reason to kick on the php engine just to run this simple script.
BTW php ---- do you want this to be a web script? If not go for the bash version, there is no reason to kick on the php engine just to run this simple script.
ASKER
da99rmd and willy134
thanks i upped the amount of points for ya now i want one more thing !
say i got multiple directories under the /var/www/html
like
/var/www/html/mrgood
/var/www/html/howareya
/var/www/html/testw
/var/www/html/wert
now i want to evaluate each one without multiple scripts because i could remove and add or rename the names of the folders at any given time !
but basically the above script should email me if one of those folders goes above 100mb !
RAISED 100points more total 150!
thanks i upped the amount of points for ya now i want one more thing !
say i got multiple directories under the /var/www/html
like
/var/www/html/mrgood
/var/www/html/howareya
/var/www/html/testw
/var/www/html/wert
now i want to evaluate each one without multiple scripts because i could remove and add or rename the names of the folders at any given time !
but basically the above script should email me if one of those folders goes above 100mb !
RAISED 100points more total 150!
ASKER
maybe some kinda for loop in there or something
we can create a for loop that loops through each directory in /var/www/html/
#!/bin/bash
USAGE=`du -s /var/www/html/ | awk '{ print $1 }'`
if [ $USAGE -ge 100000 ]; then
(echo "To: (yourmail adress here)"; echo "Subject: 100 Mb or more on html drive"; echo "Dont drink and drive." ) | sendmail -t
fi
#End
for dirname in /var/www/html/*
do
if [ -d $dirname ]; then
USAGE=`du -s /var/www/html/ | awk '{ print $dirname }'`
if [ $USAGE -ge 102400 ]; then
(echo "To: (yourmail adress here)"; echo "Subject: 100 Mb or more on $dirname"; echo "Dont drink and drive." ) | sendmail -t
fi
fi
done
--warning I did not test this because I don't have my webserver set up but I dont' know the dirname stuff works (I did test that) if da99rmd's stuff works this should work also
#!/bin/bash
USAGE=`du -s /var/www/html/ | awk '{ print $1 }'`
if [ $USAGE -ge 100000 ]; then
(echo "To: (yourmail adress here)"; echo "Subject: 100 Mb or more on html drive"; echo "Dont drink and drive." ) | sendmail -t
fi
#End
for dirname in /var/www/html/*
do
if [ -d $dirname ]; then
USAGE=`du -s /var/www/html/ | awk '{ print $dirname }'`
if [ $USAGE -ge 102400 ]; then
(echo "To: (yourmail adress here)"; echo "Subject: 100 Mb or more on $dirname"; echo "Dont drink and drive." ) | sendmail -t
fi
fi
done
--warning I did not test this because I don't have my webserver set up but I dont' know the dirname stuff works (I did test that) if da99rmd's stuff works this should work also
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
actually that is bad also I just noticed the du statement returns the wrong thing
change the USAGE line to this
USAGE=`du -s /var/www/html/$dirname | awk '{ print $1 }'`
change the USAGE line to this
USAGE=`du -s /var/www/html/$dirname | awk '{ print $1 }'`
change the USAGE line to this
USAGE=`du -s $dirname | awk '{ print $1 }'`
USAGE=`du -s $dirname | awk '{ print $1 }'`
ASKER
sorry can you reprint a working version real quick ?
and recommend a tutorial on shell scripting
and recommend a tutorial on shell scripting
ASKER
or better yet set me the way to make it appear in my logwatch daily ?
ASKER
or better yet set me the way to make it appear in my logwatch daily ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
great job ~!
its working as should !
its working as should !
And just exactly how do to that, I'm not perfectly sure right now. if you don't want to use "du -s" instead and use "if ($tmp > 100000) then mail;"