Skip to content

Instantly share code, notes, and snippets.

@hluaces
Forked from josesayago/phpcgi-watcher.sh
Last active February 20, 2019 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hluaces/d7b5833afff0dbdfb65b4578b2fbbc44 to your computer and use it in GitHub Desktop.
Save hluaces/d7b5833afff0dbdfb65b4578b2fbbc44 to your computer and use it in GitHub Desktop.
PHP-CGI Watcher, a little BASH script to kill PHP-CGI orphan processes draining server resources.
#!/bin/bash
#
# @author Jose SAYAGO
# @uri http://josesayago.com/blog/
#
# Process to monitor how many PHP-CGI processes are running, and kill them
# if they exceed the limit
#
# Set the maximum number of CGI Processes allowed
MAX_CGI=25;
# Date we will use in our logs
DATE=$(date);
# Count CGI Processes
COUNT_CGI=$(ps -ef --ppid=1 | grep /usr/bin/php-cgi | grep -v grep | wc -l);
# Exceeded our limit?
if [[ $COUNT_CGI -gt $MAX_CGI ]] ; then
# Log this
echo $DATE 'Killing PHP-CGI, ' $COUNT_CGI ' processes found. ' $MAX_CGI ' allowed.' >> /var/log/phpcgi-watcher.log ;
# Kill PHP-CGI
pkill -KILL php-cgi ;
else
# Limit not reached
echo $DATE 'System OK, there are currently ' $COUNT_CGI ' cgi processes running. ' $MAX_CGI ' allowed.' >> /var/log/phpcgi-watcher.log ;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment