#!/bin/bash
#verbose 0=none 1=logfile
verbose='1'
logfile='/root/script/check_php.log'
#logfile='/dev/stdout'
#limit is in kB (100000=100MB, 5000000=5GB)
prcL=5000000
#oom_score_adj to set when process over limit
oom_adj_score=1000
#list of processes
prcs=$(pgrep php)
for prc in $prcs; do
prcM=$(cat /proc/$prc/status|grep VmSize| cut -d ':' -f2|rev|cut -d ' ' -f2|rev|xargs)
prcP="/proc/$prc"
prcC=`cat $prcP/cmdline`
scP="$prcP/oom_score_adj"
scV=$(cat $scP)
rundate=$(date)
if [ "9$prcM" \> "9$prcL" ]; then
if [ "$verbose" -ge "1" ]; then
echo "$rundate set oom_adj_score=$oom_adj_score for PID:$prc" >> $logfile
echo " Mem VSize $prcM Kb limit is $prcL Kb" >> $logfile
detail=$(ps -q $prc -eo pid:16,cmd:50,etime:20)
echo "$detail" >> $logfile
echo >> $logfile
fi
echo $oom_adj_score >> $scP
fi
done