Awesome Useful cPanel Commands

Here are a list of useful commands for everyone under the sun to do their work easily with cPanel

cPanel Resource Usage Stats

To view cPanel’s stats you can run this command via SSH:

/usr/local/cpanel/bin/dcpumonview

This will show all processes, users, etc.

Get cPanel Resource Stats for X Days

If you want to get the stats for a user for say the past 5 days or so, run this command in SSH:

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@^<tr bgcolor=#[[:xdigit:]]+><td>(.*)</td><td>(.*)</td><td>(.*)</td><td>(.*)</td><td>(.*)</td></tr>$@Account: \1\tDomain: \2\tCPU: \3\tMem: \4\tMySQL: \5@' -e 's@^<tr><td>Top Process</td><td>(.*)</td><td colspan=3>(.*)</td></tr>$@\1 - \2@' | grep $domain -A3 ; 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

Courtesy of https://sites.google.com/site/pleskylinuxcom/bash-scripting

If you want to just monitor a specific user and not access the logs you can do so with these commands:

Monitor specific user using TOP

top -c d2 -u username

Monitor all users using TOP

top -c d2

Alternately you can use htop instead of top if you have it installed.

Script to delete Big file

#!/bin/bash
find /home -name '*.DS_Store' -type f -delete &
find /home -name '*.swp' -type f -delete &
find /home -name '*.swo' -type f -delete &
find /home -name 'error_log' -size +10M -type f -delete &
find /home -type f -name '*' -size +500M -exec rm -if {} \; &

In case you are wondering, anything bigger than 500M

find spammer script in cpanel

grep cwd /var/log/exim_mainlog | grep -v /var/spool | awk -F "cwd=" '{print $2}' | awk '{print $1}' | sort | uniq -c | sort -n

the above will look for script that is spamming your cpanel server

find 10 biggest disk user in cpanel

find /home -type d -print0 | xargs -0 du -s | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}

the above will search for the 10 biggest folder used by your user

Clear Exim Mail Queue

exiqgrep -zi|xargs exim -Mrm

this will clear all your exim queue to sparkling clean.

Delete cPanel email more than 2 years

find -P /home/*/mail/*/*/cur -mtime '+729';find -P /home/*/mail/*/*/new -mtime '+729'

firing above will delete all email that is bigger than 729 day

check for all unique ip connected to your server

netstat -atun | awk '{print $5}' | cut -d: -f1 | sed -e '/^$/d' |sort | uniq -c | sort -n

useful when you are getting DDOS

How to move all cPanel accounts to new server via command line

This is a short how-to tutorial to migrate or transfer all cPanel accounts from my old 1.5TB server to another new SSD server through command line.

Backup all cPanel accounts

i am assuming, you know what you want, so we have to first backup all the cPanel accounts in our old system using the following command

ls /var/cpanel/users | while read a; do
/scripts/pkgacct $a
done

remember to screen first before doing the above, as this might take a while if there is a lot of accounts in your machine.

Transfer all cPanel accounts

now, we need to transfer all the cPanel accounts from our old server to the new ones. Notice the pkgacct script generate all the cpmove file on /home directory, use the command below,

bash-4.1# rsync -av --progress /home/*.tar.gz [email protected]:/home

where 192.168.0.2 is your new server. Now, all the files are transferring to our new server!

Restore all cPanel accounts

Finally, in our new server, fire the following command,

ls /home/ | awk -F'[-.]' '{print $2}' | while read a; do
/scripts/killacct --user=$a
/scripts/restorepkg $a
done

similarly, remmeber to screen first before doing the above.

Change new server ip address

You might want to change the ip address of your new server to the old ones. Do the following,

To change the server's main IP address, perform the following steps:
Open the /etc/sysconfig/network-scripts/ifcfg-eth0 file with a text editor.
Edit the IPADDR and GATEWAY lines to use the IP address and gateway of your old server.
Open the /etc/ips file with a text editor.
Add your old server's primary IP address, net mask, and gateway to the file.
Note:
Remove the new server’s primary IP address from this file if it is present.
Restart the network service with the following commands:
For CentOS, CloudLinux™, and Red Hat® Enterprise Linux (RHEL) 6 and earlier, and Amazon Linux, run the service network restart command.
Note:
Amazon Linux always runs in a NAT configuration. 
 
For CentOS, CloudLinux, and RHEL 7 and later, run the systemctl restart network command.
Run the /scripts/mainipcheck command to add the IP address to the /var/cpanel/mainip file.
Run the /scripts/fixetchosts command to add the IP address and hostname of your server to the /etc/hosts file.

and you should be good to go. Test it out and enjoy your new environment!

Restart MailScanner in cPanel

Weirdly, this isn't cover so i might as well just write it down here. If you have MailScanner installed on your cPanel, the best thing to do after you've made a changes is to restart the service, but you'll find out that MailScanner isn't around as a service. So to restart cPanel as a services, you'll need to restart via cPanel script

/scripts/restartsrv mailscanner

and you'll see something like this,

-bash-4.1# /scripts/restartsrv mailscanner
Waiting for “mailscanner” to restart …………………………………………waiting for “mailscanner” to initialize ………………………………
…finished.

Service Status
	Checking MailScanner daemons:
	2049
	2050
	2111
	2264

The service “mailscanner” failed to start with the message: Checking MailScanner daemons:
2049
2050
2111
2264

Log Messages
	Jul 28 14:37:23 chopper MailScanner: MailScanner setting UID to mailnull (47)
	Jul 28 14:37:48 chopper MailScanner: Deleted 1 messages from processing-database
	Jul 28 14:37:47 chopper MailScanner: Uninfected: Delivered 1 messages
	Jul 28 14:37:47 chopper MailScanner: Delivery of nonspam: message 1ZJyQp-0007At-KW from [email protected] to [email protected] with subject  [Emporium Blog 2] Please moderate: "Paypal Utilizes Credit Card Machinery for Malaysians to Withdraw Funds"

mailscanner has failed. Please contact your system administrator if the service does not automagically recover.

You'll notice that the service mentioned that mailscanner has failed, just ignore it. As long as the daemons is up, it's kicking.

Installing WordPress using cpAddons – You do not have sufficient permissions to access this page.

I have a problem today using cpAddons to install my WordPress into one of my subdirectory. I receive "You do not have sufficient permissions to access this page." error upon logging into my WordPress panel. I believe this is not the first time i encounter this problem but this problem only appear if you are not using the default database "wp" as suggested by cpAddons. I google around for a while but didn't get the answer i want or any valuable solution. Hence, i debugged it myself.

Apparently,  everything generated by cpanel cpAddons was correct. The database data and wp-config.php was set correctly. Even the database user permission was also correctly given.  The file permission of wp-config.php was correct.

Now, i remove all the tables generated by cpAddons and run through WordPress configuration page and setup the WordPress installation again. This will install all the tables that i just deleted into the database. Guess what? It works. This means that the database tables installed by cpAddons is a buggy one. Therefore, we will have to erase the tables through phpmyadmin so that WordPress can be install correctly.

Move cpanel mysql to other directory safely

This is also for my personal need in the future whenever i setup a cpanel server for my TWOPIZ HOSTING. There is a way to move mysql to other directory other than the one defined by cpanel. Firstly, you will need to backup everything on /var/lib/mysql folder. Once you have done that, you will have to follow the below instruction.


cd /var/lib
service chkservd stop
service mysql stop
mv mysql /home
ln -s /home/mysql mysql
service mysql start
service chkservd start

The backup apps will still work normally as the mysql folder still exist on /var/lib just that now it is pointing to the /home directory where the size is the largest.