Solved

linux shell script

Posted on 2004-03-29
15
705 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 20 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 130 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

726 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