From f48a22c34540a1fd1a9cf2ec3310ece72f344c92 Mon Sep 17 00:00:00 2001 From: Christoph Date: Mon, 25 Jun 2018 02:07:33 +0200 Subject: [PATCH] Add script 'check_zombies_php-cgi.sh'. --- check_zombies_php-cgi.sh | 132 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100755 check_zombies_php-cgi.sh diff --git a/check_zombies_php-cgi.sh b/check_zombies_php-cgi.sh new file mode 100755 index 0000000..3dd5dcc --- /dev/null +++ b/check_zombies_php-cgi.sh @@ -0,0 +1,132 @@ +#!/usr/bin/env bash + + +#--------------------------------------- +#----------------------------- +# Settings +#----------------------------- +#--------------------------------------- + +# - Is this script running on terminal ? +# - +if [[ -t 1 ]] ; then + terminal=true + LOGGING=true +else + terminal=false + LOGGING=false +fi + + +KILL_SIGNAL=HUP + + +#--------------------------------------- +#----------------------------- +# Base Function(s) +#----------------------------- +#--------------------------------------- + +fatal(){ + echo "" + echo -e " [ Fatal ] $*" + echo "" + echo -e "\tScript terminated.." + echo "" + exit 1 +} + +error (){ + echo "" + echo -e " [ Error ] $*" + echo "" +} + +warn (){ + echo "" + echo -e " [ Warn ] $*" + echo "" +} + +info (){ + echo "" + echo -e " [ Info ] $*" + echo "" +} + +ok (){ + echo "" + echo -e " [ Ok ] $*" + echo "" +} + + +#--------------------------------------- +#----------------------------- +# Check some prerequisites +#----------------------------- +#--------------------------------------- + +if [ -z "`which basename`" ]; then + fatal 'It seems "basename" is not installed, but needed!' +fi + +if [ -z "`which realpath`" ]; then + fatal 'It seems "realpath" is not installed, but needed!' +fi + +php_cgi=$(realpath $(which php-cgi) 2>/dev/null ) > /dev/null 2>&1 + +if [[ -z "$php_cgi" ]]; then + if [[ -x "/usr/local/php/bin/php-cgi" ]]; then + php_cgi="/usr/local/php/bin/php-cgi" + else + fatal "No 'php-cgi' binary found." + fi +fi + + +#--------------------------------------- +#----------------------------- +# Jobhandling +#----------------------------- +#--------------------------------------- + +#LOCK_DIR=`mktemp -d` + +## - Remove lockdir when the script finishes, or when it receives a signal +#trap 'rm -rf "$LOCK_DIR"' 0 2 15 + + +#--------------------------------------- +#----------------------------- +# Checking .. +#----------------------------- +#--------------------------------------- + + +if $LOGGING ; then + echo -e "\nChecking for zombie processes \"$php_cgi\" on \"`hostname -f`\".." +fi +if `ps -Ao"stat,command" | grep "$(basename $php_cgi)" | grep -v grep | grep -e '^[zZ]' > /dev/null 2>&1` ; then + warn "Found Zombie Processes \"$php_cgi\" on \"`hostname -f`\":\n" + ps -Ao"stat,user,pid,ppid,command" | grep "$(basename $php_cgi)" | grep -v grep | grep -e '^[zZ]' + membefore=`cat /proc/meminfo | grep Committed_AS | awk '{print $2}'` + info "Trying a graceful kill to the concerning parents with signal \"$KILL_SIGNAL\".." + ps -Ao"stat,pid,ppid,command" | grep "$(basename $php_cgi)" | grep -v grep | grep -e '^[zZ]' \ + | awk '{ print $3 }' | sort -u | xargs --no-run-if-empty kill -$KILL_SIGNAL + if [ "$?" = "0" ];then + sleep 10 + ok "Cleaning up zombie processes \"$php_cgi\" seems succsessfully finisched" + memafter=`cat /proc/meminfo | grep Committed_AS | awk '{print $2}'` + info "`expr $membefore - $memafter` KB RAM has been freed" + else + error "Cleaning up zombie processes \"$php_cgi\" failed!" + fi +else + if $LOGGING ; then + ok "No zombie prossses found" + fi +fi + +exit 0