-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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