Linux server memory footprint too big solution

Source: Internet
Author: User
Tags fpm php script sleep server memory pkill

Today to the Company by convention to check the status of all servers, found that the memory of a server is much larger than usual, the following figure

Immediately QQ asked the next program development whether the last 1-2 days changed something, in the case that did not change what, immediately restart the next php-fpm and Nginx, after the restart looked at, memory released 1G, but this is not good, because this server is 8G memory, usually most use 4G more appearance, Want to use today to 7G much faster to take up the server memory, it is obviously not normal, continue to check.


During the check, I used the top to see which process consumed the most memory, saw nothing for nearly 1 minutes, and then used the command to view the 10 processes that consumed the most memory:

The code is as follows Copy Code

Ps-aux | Sort-k4nr | Head-n 10

Also just shows the Nginx and PHP-FPM occupy the largest memory, but I restarted the two processes, it seems to have nothing to do with their 2, I began to suspect that the server is not black, immediately look at the process PS aux, this look found the problem, there is a script in the process of a lot of production, Execute the following command immediately to kill:

The code is as follows Copy Code
Kill-9 $ (PS aux | grep process name |GREP-V grep| awk ' {print $} ')

Now look at memory usage, memory is finally normal, OK, and then observe a few minutes, no problem out of the server, continue to check the problem of the script.

PS: The thing to emphasize here, do not according to their intuition to casually think what is the problem, it is best to investigate the implementation of the clear, for example, I am here is based on their intuition that the problem is nginx and php-fpm, fortunately with the eldest brother to greet the restart, otherwise the consequences are very serious.

and give you a command to view the process with the largest memory footprint:

The code is as follows Copy Code
PS aux| Grep-v "USER" |sort-n-r-k 4 |awk ' nr==1{print $} '

I'm using a PHP script. Here we have a Linux script that we're writing Linux to restart when the load is too high

We can according to their own server situation to decide to load more than how much to restart PHP, I only set the load to 1 on the restart PHP.


System: CentOS 5.9 64-bit
Script content:
VI check-php.sh

The code is as follows Copy Code

#!/bin/sh
Top_sys_load_num=1
Sys_load_num= ' Uptime | awk ' {print $ (NF-2)} ' | Sed ' s/,//'

echo $ (date + "%y-%m-%d") ' Uptime '
If [' Echo ' $TOP _sys_load_num < $SYS _load_num "|BC '-eq 1]
Then
echo "#0 #" $ (date + "%y-%m-%d%h:%m:%s") "Pkill php-fpm" Ps-ef | grep php-fpm | Wc-l '
/ETC/INIT.D/PHP-FPM stop
Sleep 8
Pkill PHP-FPM
Sleep 8
For I in 1 2 3
Todo
If [' Pgrep php-fpm | wc-l '-le 0]
Then
/ETC/INIT.D/PHP-FPM start
Sleep 30
echo "#1 #" $ (date + "%y-%m-%d%h:%m:%s") "Start php-fpm" Ps-ef | grep php-fpm | Wc-l '
Fi
Done
Else
If [' Pgrep php-fpm | wc-l '-le 0]
Then
/ETC/INIT.D/PHP-FPM start
Sleep 30
echo "#2 #" $ (date + "%y-%m-%d%h:%m:%s") "Start php-fpm" Ps-ef | grep php-fpm | Wc-l '
Fi
Fi

Timed execution

The code is as follows Copy Code
*/3 * * * */root/shell/check-php.sh >>/var/log/check-php.log

If you run a script that appears:

The code is as follows Copy Code
: Line 6:bc:command not found
: Line 6: [:-eq:unary operator expected

Indicates no BC command, CentOS can be installed with yum-y install BC.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.