Solved

linux shell script

Posted on 2004-03-29
15
679 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
  • 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
 
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now