Once again I needed a solution to kill php-fpm or apache2 process with the high CPU usage and considering that I am not at the first similar problem I said that it is good to write it again somewhere, not to forget it. And this time the problem appeared with sites in Wordpress, it is a pretty serious problem and often occurs when using Woocomerce as a plugin.

php-fpm or apache2 process with the High CPU in Linux

So if you ever have very high CPU usage when Woocommerce is enabled in Wordpress or in other similar situations, you can rely on the solution I presented here.

Step 1. Create an executable shell script file that will run automatically every few minutes:
@ sudo nano /home/killhicpu.sh

Depending on what you use on the server: apache2 + php or nginx + php-fpm + apache2, you have three options to choose from.

Write one of the following command lines in the killhicpu.sh file:

1. only for apache2 + php
sudo top -b -s -n 1 | grep apache2 | awk '{if ($9 > 90) print $1}' | while read line; do sudo kill -9 "$line"; date; echo "-"; done | tee -a /home/kill.log
2. only for nginx + php-fpm + apache2
sudo top -b -s -n 1 | grep php-fpm | awk '{if ($9 > 90) print $1}' | while read line; do sudo kill -9 "$line"; date; echo "-"; done | tee -a /home/kill.log
3. solution that covers both options, it is also the variant I prefer:
sudo top -b -s -n 1 | grep -E '(php-fpm|apache2)' | awk '{if ($9 > 90) print $1}' | while read line; do sudo kill -9 "$line"; date; echo "-"; done | tee -a /home/kill.log

save the file and then make it executable:

@ sudo chmod +x /home/killhicpu.sh

There is only one step left, you have to run this script every minute or at least once every 5 minutes.

Step 2. Add Jobs to cron under Linux as follows:
@ sudo crontab -e

add the following command line to the end of the file, which will run once a minute:

* * * * * /home/killhicpu.sh

alternatively, if desired for every 5 minutes, the command line is as follows:

*/5 * * * * /home/killhicpu.sh

linux-cronjob-crontab-command

Save the file [F2], confirm the change and that’s it!

If a php-fpm or apache2 process happens to load the processor too much, it will be forcibly terminated and the action will be logged in the /home/kill.log file.

Thu Dec 24 23:13:02 UTC 2020
-
Thu Dec 24 23:13:02 UTC 2020
-
Thu Dec 24 23:13:02 UTC 2020
-
Thu Dec 24 23:13:02 UTC 2020
-
Fri Dec 25 00:19:01 UTC 2020
-

Daca ne uitam in logurile php (le gasiti in /var/log/) o sa vedem mai multe detalii despre activitatea acestui script:

[26-Dec-2020 01:13:01] WARNING: [pool demodomain3141592.com] child 3075 exited on signal 9 (SIGKILL) after 0.402922 seconds from start
[26-Dec-2020 01:13:01] NOTICE: [pool demodomain3141592.com] child 3088 started
[26-Dec-2020 10:59:02] WARNING: [pool demodomain3141592.com] child 3963 exited on signal 9 (SIGKILL) after 19.068475 seconds from start
[26-Dec-2020 10:59:02] NOTICE: [pool demodomain3141592.com] child 3977 started
[26-Dec-2020 13:12:01] WARNING: [pool demodomain3141592.com] child 2505 exited on signal 9 (SIGKILL) after 26.088094 seconds from start
[26-Dec-2020 13:12:01] NOTICE: [pool demodomain3141592.com] child 2555 started

As you can see, the processes will not block the processor for hours, draining the entire server of resources, but they are finished in maximum 5 minutes (if the cron interval is configured this way)!

Here, for example, are the logs after the first run, when the script “killed” processes that kept the cores locked for a very long time, as you can see one of the processes was practically blocked for more than 10 hours:

24-Dec-2020 23:13:02] WARNING: [pool demodomain3141592.com] child 419 exited on signal 9 (SIGKILL) after 572.506945 seconds from start
[24-Dec-2020 23:13:02] NOTICE: [pool demodomain3141592.com] child 2284 started
[24-Dec-2020 23:13:02] WARNING: [pool demodomain3141592.com] child 1175 exited on signal 9 (SIGKILL) after 15541.027303 seconds from start
[24-Dec-2020 23:13:02] NOTICE: [pool demodomain3141592.com] child 2289 started
[24-Dec-2020 23:13:02] WARNING: [pool demodomain3141592.com] child 2138 exited on signal 9 (SIGKILL) after 36960.932477 seconds from start
[24-Dec-2020 23:13:02] NOTICE: [pool demodomain3141592.com] child 2293 started
[24-Dec-2020 23:13:02] WARNING: [pool demodomain3141592.com] child 3413 exited on signal 9 (SIGKILL) after 18870.801298 seconds from start
[24-Dec-2020 23:13:02] NOTICE: [pool demodomain3141592.com] child 2297 started

It is not a solution that solves the problem but only a good, tested patch that works. The real problem – for my case –  lies in Woocommerce and Wordpress, the way they serve pages when “bad bots” crawling the site, plus the problem of WP-Cron, that service runs at every load of the site !!! and sometimes it enters an infinite loop from which it never comes out, and this is bad, sometimes very bad, because it can block even the most powerful server.

Websites with Woocommerce can be the target of a DDOS attack and will fall very quickly, sometimes even ssh access doesn’t work anymore, so you will be blocked off the server and without a reset from the hosting-administration page you have nothing to do with it!

The conclusion is only one and clear:

Stop using plugins that overuse the wordpress cron because sooner or later you will have serious problems !

Tags: , , , , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *