Shell Script for Disk Space Notification

November 13, 2009    |   Posted by admin   |    Category: Scripts

Shell Script for Disk Space Notification

The 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.

Create a file /home/diskspace.sh and paste the following code in it:

#!/bin/sh

#Threshold is set to 70 here.
THRESHOLD=70
MAILTO=”youremailaddress”
TEMPFILE=/tmp/diskspace.temp
HOSTNAME=`hostname`

rm -f $TEMPFILE

#Calculate the Current Disk Usage with the below command.
CDU=$(df -h | tail -1 | awk ‘{print $5}’ | sed ‘s/%//’)

#Compare the current value with the threshold one.
if [ $(expr $CDU “>=” $THRESHOLD) -ne 0 ]
then
echo “Warning!!! Disk Space usage on server $HOSTNAME is ${CDU}%” >> $TEMPFILE
fi

#Send an email if /tmp/diskspace.temp is present.
if [ -e $TEMPFILE ]
then
mail -s “Disk Space Notification” $MAILTO < $TEMPFILE
fi

rm -f $TEMPFILE

Save the file. Now you need to schedule a cron to execute the script once in a week.

crontab -e

and set the following cronjob:

0 1 * * 0  /bin/sh  /home/diskspace.sh

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.

Correcting ownership of all cPanel Users

September 28, 2009    |   Posted by admin   |    Category: Scripts

If you mess up with the ownership of all the users on a cPanel server, following script will help you to correct the ownership:

#  for i in `cat /etc/trueuserdomains | awk ‘{print $2}’`
> do
> /bin/chown $i.$i /home/$i -R;

> /bin/chown $i.mail /home/$i/etc -R;
> /bin/chown $i.nobody /home/$i/public_html;
> done;

The ownership of the home directory of a user, etc and public_tml will be corrected in a one go.

Comments Off on Correcting ownership of all cPanel Users