Skip to content

Instantly share code, notes, and snippets.

@josesayago
Last active February 23, 2021 14:31
Show Gist options
  • Save josesayago/38749e0da0beb3c77ca1 to your computer and use it in GitHub Desktop.
Save josesayago/38749e0da0beb3c77ca1 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 | 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