{"id":325,"date":"2009-11-13T14:19:09","date_gmt":"2009-11-13T18:19:09","guid":{"rendered":"http:\/\/linuxhostingsupport.net\/blog\/?p=325"},"modified":"2013-03-20T08:41:10","modified_gmt":"2013-03-20T12:41:10","slug":"shell-script-for-disk-space-notification","status":"publish","type":"post","link":"https:\/\/linuxhostingsupport.net\/blog\/shell-script-for-disk-space-notification","title":{"rendered":"Shell Script for Disk Space Notification"},"content":{"rendered":"<blockquote><p><span style=\"color: #0000ff;\"><strong> Shell Script for Disk Space Notification<\/strong><\/span><\/p><\/blockquote>\n<p>The <span style=\"color: #0000ff;\"><strong>Disk Space Notification script<\/strong><\/span> can be scheduled to run once in a week which will check the Disk Space Usage on the server. The Shell script will send out an email to the admin if the Disk Space Usage is greater than the Threshold.<\/p>\n<p><strong>Create a file \/home\/diskspace.sh<\/strong> and paste the following code in it:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\"><strong>#!\/bin\/sh<\/strong><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><strong>#Threshold is set to 70 here.<br \/>\nTHRESHOLD=70<br \/>\nMAILTO=&#8221;youremailaddress&#8221;<br \/>\nTEMPFILE=\/tmp\/diskspace.temp<br \/>\nHOSTNAME=`hostname`<\/strong><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><strong>rm -f $TEMPFILE<\/strong><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><strong>#Calculate the Current Disk Usage with the below command.<br \/>\nCDU=$(df -h | tail -1 | awk &#8216;{print $5}&#8217; | sed &#8216;s\/%\/\/&#8217;)<\/strong><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><strong>#Compare the current value with the threshold one.<br \/>\nif [ $(expr $CDU &#8220;&gt;=&#8221; $THRESHOLD) -ne 0 ]<br \/>\nthen<br \/>\necho &#8220;Warning!!! Disk Space usage on server $HOSTNAME is ${CDU}%&#8221; &gt;&gt; $TEMPFILE<br \/>\nfi<\/strong><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><strong>#Send an email if \/tmp\/diskspace.temp is present.<br \/>\nif [ -e $TEMPFILE ]<br \/>\nthen<br \/>\nmail -s &#8220;Disk Space Notification&#8221; $MAILTO &lt; $TEMPFILE<br \/>\nfi<\/strong><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><strong>rm -f $TEMPFILE<\/strong><\/span><\/p><\/blockquote>\n<p>Save the file. Now you need to <strong>schedule a cron to execute the script<\/strong> once in a week.<\/p>\n<blockquote><p><span style=\"color: #0000ff;\"><strong>crontab -e<\/strong><\/span><\/p><\/blockquote>\n<p>and set the following cronjob:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\"><strong>0 1 * * 0\u00a0 \/bin\/sh\u00a0 \/home\/diskspace.sh<\/strong><\/span><\/p><\/blockquote>\n<p>Save the file and restart the crond service. The cronjob will now execute on Sunday at 1.00AM and will send a notification if the disk space usage on the server is above the threshold.<\/p>\n","protected":false},"excerpt":{"rendered":"<p> Shell Script for Disk Space Notification<br \/>\nThe Disk Space Notification script can be scheduled to run once in a week which will check the Disk Space Usage on the server. The Shell script will send out an email to the admin if the Disk Space Usage is greater than the Threshold.<br \/>\nCreate a file \/home\/diskspace.sh and paste the following code in it:<br \/>\n#!\/bin\/sh<br \/>\n#Threshold is set to 70 here.<br \/>\nTHRESHOLD=70<br \/>\nMAILTO=&#8221;youremailaddress&#8221;<br \/>\nTEMPFILE=\/tmp\/diskspace.temp<br \/>\nHOSTNAME=`hostname`<br \/>\nrm -f $TEMPFILE<br \/>\n#Calculate the Current Disk Usage with the below command.<br \/>\nCDU=$(df -h | tail -1 | awk &#8216;{print $5}&#8217; | sed &#8216;s\/%\/\/&#8217;)<br \/>\n#Compare the [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[106],"tags":[402,401,400,404,403,405,399],"_links":{"self":[{"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/posts\/325"}],"collection":[{"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/comments?post=325"}],"version-history":[{"count":3,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/posts\/325\/revisions"}],"predecessor-version":[{"id":327,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/posts\/325\/revisions\/327"}],"wp:attachment":[{"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/media?parent=325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/categories?post=325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxhostingsupport.net\/blog\/wp-json\/wp\/v2\/tags?post=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}