Added on Feb 1st, 2015 and marked as cli server

Recently I had a problem with restarting php5-fpm on an Ubuntu machine. This is not something I need to do often, when php5-fpm is running you can pretty much leave it alone. But once in a while I need to add a new pool, and in that case a restart is required.

Normally you would restart the service with:

service php5-fpm restart

But somehow this did not kill the pools and therefore nothing really happened. At first I assumed a simple killall php-fpm would do the trick, but alas: php-fpm: no process found. I had to manually kill all processes that the pools were using and then start the service again! With just a couple of pools, it’s not that terrible, but when the number of pools increases and you have several dozens of active pools (with multiple processes per pool) this is no longer a feasible job.

Luckily in my case, all pools were running as the web-user www-data. So by using some ps and awk it is possible to get a list of the process IDs and use this to kill the processes:

kill `ps aux|grep php-fpm | grep www-data | awk '{print $2}'`

And of course, adding a -9 would help if you had to force a kill.

When these processes are killed, it is possible to start the service again and the pools would become active again.

Example

A simple little script to kill all PHP5-FPM pools and start fresh.

#!/bin/bash
# Try to restart PHP5-FPM in the regular way.
# If unsuccesfull, then kill all the pools and start
# the service again
service php5-fpm restart
if [ $? -ne 0 ]; then
echo 'Unable to restart php5-fpm'
echo 'Kill all php5-fpm pools'
kill `ps aux|grep php-fpm | grep www-data | awk '{print $2}'`
echo 'Start php5-fpm'
service php5-fpm start
fi

This script does exactly what I need when a new pool must be activated. Simply running this script will first check if php5-fpm could be restarted and, if not, kills all php-processes and starts the service. Super fast and easy.