Solved

linux shell script

Posted on 2004-03-29
15
693 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINUX backups with VEEAM 8 118
open source backup solution 1 36
000webhost.com default error log 1 40
Intel fortran compiler (ifort) 5 38
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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…
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…

839 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