?
Solved

linux shell script

Posted on 2004-03-29
15
Medium Priority
?
709 Views
Last Modified: 2010-04-20
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;
?>
0
Comment
Question by:aot2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 7

Expert Comment

by:troopern
ID: 10711369
Uour PHP script doesn't work, because you need to cut out the trailing M/G in your "du -hs /var/www/html/" result.
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;"
0
 
LVL 7

Expert Comment

by:troopern
ID: 10711375
Your* ... alittle typo there.
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 10711998
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
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 5

Expert Comment

by:willy134
ID: 10714416
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.
0
 
LVL 1

Author Comment

by:aot2002
ID: 10714779
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!
0
 
LVL 1

Author Comment

by:aot2002
ID: 10714794
maybe some kinda for loop in there or something
0
 
LVL 5

Expert Comment

by:willy134
ID: 10716228
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

0
 
LVL 5

Assisted Solution

by:willy134
willy134 earned 80 total points
ID: 10716235
sorry I duplicated stuff up top it should only be...

ated stuff up top it should only be...

#!/bin/bash
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
#end
0
 
LVL 5

Expert Comment

by:willy134
ID: 10716302
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 }'`

0
 
LVL 5

Expert Comment

by:willy134
ID: 10716386
change the USAGE line to this

 USAGE=`du -s $dirname | awk '{ print $1 }'`
0
 
LVL 1

Author Comment

by:aot2002
ID: 10720842
sorry can you reprint a working version real quick ?
and recommend a tutorial on shell scripting
0
 
LVL 1

Author Comment

by:aot2002
ID: 10721551
or better yet set me the way to make it appear in my logwatch daily ?
0
 
LVL 1

Author Comment

by:aot2002
ID: 10721552
or better yet set me the way to make it appear in my logwatch daily ?
0
 
LVL 8

Accepted Solution

by:
da99rmd earned 520 total points
ID: 10721599
This is the working version
#!/bin/bash
for dirname in /var/www/html/*
do
      if [ -d $dirname ]; then
           USAGE=`du -s $dirname | awk '{ print $1 }'`
         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
#end

Here are som bash scripting howtos, dont just forget the man page man bash its usefull too.
http://www.tldp.org/LDP/abs/html/
and
http://www-106.ibm.com/developerworks/linux/library/l-bash.html
0
 
LVL 1

Author Comment

by:aot2002
ID: 10730580
great job ~!

its working as should !
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question