How to kill all PHP-FPM processes in a single command line
Shutting down php-fpm often feels like herding cats when you aren't using a systemd service or supervisor script. The core issue is that php-fpm runs multiple worker processes per pool, and a standard stop signal often only hits the master process. If the master dies but the workers linger in a zombie state or fail to restart on the next attempt, your application becomes unresponsive.
To solve this, you need a command that targets every PID associated with the php-fpm binary. The most reliable way to do this without installing extra tools is by combining process inspection with a looped kill signal.
Identifying the Targets
The first step is understanding what you are looking at in your process list. As seen in the sample output from ps -aux | grep php-fpm, each line represents a distinct worker. You can see multiple processes sharing the same Parent PID (like the entries under PID 794, 795, and so on), which confirms that these are children of a single master process spawned for specific pools like tokyodating.co or ukrainedating.co.
When you attempt to stop the service, the signal usually travels down to the master. However, if your custom init script or systemd unit fails to send a graceful termination sequence, those child processes can remain active. This is why a simple kill -TERM $(pgrep php-fpm) might leave half the fleet standing.
The One-Liner Solution
To ensure every worker and the master process are terminated, you should target the command name directly rather than relying on partial name matching that might catch unrelated scripts. The most robust approach uses pkill with a specific regex pattern or iterates through the PIDs of processes owned by the PHP-FPM user.
Run the following command to send a termination signal to all instances:
pkill -9 php-fpm
This command sends a SIGKILL (signal 9) to every process where the executable name matches php-fpm. This is aggressive but necessary if graceful shutdowns have failed. If you prefer to preserve any ongoing requests for a few seconds before killing, use -15 instead:
pkill -15 php-fpm && pkill -9 php-fpm
The second command attempts a graceful termination first. If the process does not exit within the default timeout (which varies by implementation but is usually immediate for SIGKILL), it follows up with a hard kill. Note that pkill matches against the full path if no executable name is found in /proc, so this works regardless of whether PHP-FPM was installed via binary, source, or package manager.
Integrating into init.d Scripts
If you are writing a custom init script, placing this logic in the stop function ensures a clean slate before restart attempts. Your script should look something like this:
case "$1" in
start)
echo "Starting PHP-FPM pools..."
# Start command logic here
;;
stop)
echo "Stopping all PHP-FPM processes..."
pkill -9 php-fpm
sleep 2
echo "PHP-FPM stopped."
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
The sleep 2 is a safety buffer. Even though SIGKILL forces an exit, the filesystem (like logs or sockets) might need a moment to close handles cleanly before you attempt to bind ports or restart the service again.
Pitfalls and Considerations
Be cautious when using pkill -9. It bypasses the application's cleanup routines. If your PHP-FPM pools are handling long-running tasks like cron jobs scheduled via pm.max_children, those tasks will be cut off immediately. This might leave temporary files in the upload directory or incomplete database transactions.
Additionally, ensure you are not killing other processes that happen to contain "php-fpm" in their command line argument but aren't actually the FPM daemon. Using --exact with pkill (available on GNU coreutils) helps avoid false positives:
pkill --exact php-fpm
This restricts the match to processes where php-fpm is the sole executable name, preventing accidental kills of wrapper scripts or monitoring daemons that invoke PHP-FPM.
For production environments managed by systemd, you can also configure a custom stop script referenced in your unit file to execute this logic before the service stops completely. This ensures that even if the default signal handling fails, your explicit command runs first.