How to free PHP-fpm Memory at Server

 

Often when you are running PHP with any web server (Apache or Nginx), the FastCGI process manager eats up a lot of your RAM, by forking multiple subprocesses for every request, which in turn leads to memory leakage.

To avert this, it is desirable to schedule a shell script and reduce this memory leakage.

Save the below command as “.sh” file and schedule that on cron each day.

#!/bin/bash

FREE=$(free -mt | grep Total | awk ‘{print $4}’)

if [ $FREE -lt 200 ] ;then
echo -e “`date ‘+%b %d %H:%M:%S’` `hostname` MEMUSAGE alert – low free RAM – free mem = $FREE MB” >> /var/log/sysops-sh/free-mem.log
/etc/init.d/php-fpm reload
RETVAL=$(echo $?)
if [ $RETVAL -eq 0 ]; then
FREE2=$(free -mt | grep Total | awk ‘{print $4}’)
echo -e “`date ‘+%b %d %H:%M:%S’` `hostname` PHP-FPM reloaded successfully.Free Mem = $FREE2 MB” >> /var/log/sysops-sh/free-mem.log
fi
fi

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s