Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rickytato/ed77338d3e3c52fd7db607507aac800a to your computer and use it in GitHub Desktop.
Save rickytato/ed77338d3e3c52fd7db607507aac800a to your computer and use it in GitHub Desktop.
Set PHP-FPM pool workers CPU affinity so each worker gets one CPU core
#!/bin/bash
# Set PHP-FPM pool workers CPU affinity so each worker gets one CPU core
# Note that FPM recycles threads, so you'll need to run this on cron periodically
# Author: Steve Kamerman
CPUS=$(grep -c processor /proc/cpuinfo)
FPM_PIDS=$(ps auxw | grep php-fpm | grep pool | awk '{ print $2; }')
if [ "$FPM_PIDS" = "" ]; then
echo "PHP-FPM is not running"
fi
typeset -i i
let i=0
for PID in $FPM_PIDS; do
THIS_CPU=$(echo "$i % $CPUS" | bc)
taskset -pc $THIS_CPU $PID
let i++
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment