Blog

Login Register

Orphaned PHP processes and FCGID

Sept 6: CHANGED: print $2 instead of print $3 would use PID correctly (as it was originally there)
Sept 3: CHANGED: Added path for all the commands, in case PATH variable is not set
Oct 29: CHANGED: unified command to be run on any server panel switching 'pid' order and killing $1, add no-run-if-empty to xargs


Over the last year we had several people coming back to us, saying that mod_fcgid would eventually create hundreds (or thousands) PHP processes, eat up all the memory and crash the system.
What was strange that neither of mod_fcgid settings like time to leave, or max number of processes wouldn't help. The issue would affect all versions of PHP from 5.1 to 5.4. What was happening is that at some point PHP processes would stop having Apache as their parent, and their PPID would become 1. They would sleep, and waste memory. They would not die, and have to be killed to free up memory. The same issue affects CentOS and CloudLinux servers -- so we knew it is not a side effect of LVE. Upgrading mod_fcgid to latest version doesn't help as well. So, while we don't have a true resolution for the problem, we created simple work around -- where a cron job would kill all the orphaned PHP processes once every few minutes (we recommend doing it every 5 minutes or so).
The command kills only orphaned PHP processes with PPID=1

/bin/ps -Ao"pid,command,ppid"|/bin/grep ' 1$'|/bin/grep /php|/bin/awk '{ print $1; }'|/usr/bin/xargs --no-run-if-empty kill -9 
Adding this command to crontab, solves the problem of PHP processes wasting memory.If you want to see if you have orphaned php processes - run:

ps -Ao"pid,command,pid"|grep ' 1$'|grep /php

0
Richard Hordern
11/27/2012 05:53:30
Command prints error when result is empty
We added --no-run-if-empty to the xargs commande :


/bin/ps -Ao"command,pid,ppid"|/bin/grep ' 1$'|/bin/grep /php|/bin/awk '{ print $2; }'|/usr/bin/xargs --no-run-if-empty kill -9
Link 0