Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday, 24 January 2014

Reducing CPU Load in Server

Initial Check up :

To print the cpu load
# cat /proc/loadavg

To see the No: of user logged in server ,
#

To see the uptime and load average
# uptime

To see live process running
# top -c

To print  process current process
# ps aux

To filter user/process/pid from process, type
# ps aux | grep <user/process/pid>

If the cpu load is controllable ,just check the process list and filter the user which taking more resource of the server.

To kill the Process Id(PID) by
# pkill -9 <PID>

To kill the user By
# pkill -9 -u <username>

To kill the service , type
# killall -9 <service>
 Example:  # killall -9 php


To kill a file , type

# fuser -k <file_path>

Example:   # fuser -k  /usr/bin/php


Following command will find top 5 users in server.

# ps faux |grep -i 'p[h]p\|.pl\|perl\|dispatch' | grep -v 'ftp\|imap\|nobody\|mailnull\|root\|mysql\|cpanel\|32004\|32021' |  awk '{print $1}'  | sort | uniq -c | sort -nr | head -n 5

Following command will kill top 2 users in server


# for i in `ps faux |grep -i 'p[h]p\|.pl\|perl\|dispatch' | grep -v 'ftp\|imap\|nobody\|mailnull\|root\|mysql\|cpanel\|32004\|32021' |  awk '{print $1}'  | sort  | uniq -c  | sort -nr |head -n 2|awk '{print$2}'` ; do echo $i; killall -u $i; cat /proc/loadavg ; done


If the sever load is high , execute the commands

To kill the process using for loop ,

# for i in { 1..100 } ; do killall -9 php ; cat /proc/loadavg ; done
 or
 # for i in { 1..100 } ; do fuser -k /usr/bin/php ; cat /proc/loadavg ; done

In some cases ,users/process in the server may be the cause for server load , filter the user and run the command,
# ps aux | grep <user/process_name> | awk {'print $2'} | xargs kill -9 
If the server load is again increasing , stop the service which using  the server resource load , By executing the command ,

# /etc/init.d/<service> stop
Example:  # /etc/init.d/exim stop

The above command is not recommended to execute , Examine the process and block the unwanted   IPs.


Command to find sorted memory usage in server,

 # ps aux | awk '$11!~/\[*\]/ {print $6/1024" Mb --> "$11,$12,$13,$14}' | sort -g

 Troubleshooting wait average [Amount of time the CPU has been waiting for I/O to complete.]

# for x in `seq 1 1 10`; do ps -eo state,pid,cmd | grep "^D"; echo "----"; sleep 5; done 

Important - PLEASE do the below command if the load is increasing heavily ,

# ps aux |egrep "clam|php|find|maldet|exim|spam|lfd" | awk '{print $2}' | xargs kill -9

The above command will kill the processes specified in "egrep command" , add or remove the process according to the status of the server .


Please give the name in the '<user>' field of below script  to get process run by the user:


# user=<username> ;for blah in seq; do echo ; uptime;echo; echo "Running Proceses:" ;echo;for pass in 1 2 3 4; do echo "---Pass $pass---"; x=1 ; while [ $x -lt 2 ]; do ps faux | grep "^${user}" ; [ $? -eq 0 ] && echo "The above processes were found to be running for user ${user} when this was executed by  Stebu Johny during each pass at a 2 second interval `date` ..." || echo "No processes were found to be running for user ${user} when this was executed by  Stebu Johny during each pass at `date` ..."; sleep ".2"; let x++ ;done;echo;sleep 2;done; echo "MySQL Processes:";mysqladmin proc --verb | grep "${user}_" | perl -p -e 's/\s+\|/\|/g' | grep "" ; [ $? -eq 0 ] && echo "The above queries were found to be running for user ${user} when this was executed by  Stebu Johny at `date` ..." || echo "No queries were running for user ${user} when this was executed by  Stebu Johny at `date` ..."; echo $'\n'; echo "Incoming Connections:"; lynx --dump --width=10000 localhost/whm-server-status | grep "GET\|POST" | awk '{print $11,$12,$14}' | column -t | sort | grep "/~$/|Client\|$(grep ": ${user}$" /etc/userdomains | cut -d : -f1 | cut -c 1-31)"; echo; echo; done











No comments:

Post a Comment