檢測某個php進程shell腳本
一.主要shell代碼
#!/bin/bash
executePath=$(readlink -f $(dirname $0))
executeLog=`readlink -f $executePath/check.log`
envPath=`readlink -f $executePath/../../env.sh`
source $envPath
set -a; source /etc/environment; set +a;
scriptIndex=`readlink -f $executePath/../console/index.php`
proccessName=common/test/index
proccessNum=`ps aux|grep -v grep|grep $proccessName|wc -l`
needNum=1
if [ $1 ]
then
proccessName=$1
fi
if [ $2 ]
then
needNum=$2
fi
function setProccessNum
{
proccessNum=`ps aux|grep -v grep|grep $proccessName|wc -l`
}
function stop_pro()
{
pids=$(ps aux|grep -v grep|grep common/test/index|awk '{ print $2 }')
for pid in $pids
do
setProccessNum
if [ $proccessNum -eq $needNum ]
then
break
fi
kill -9 $pid
done
}
function start_pro()
{
setProccessNum
while (( $proccessNum < $needNum ))
do
/usr/local/php/bin/php $scriptIndex $proccessName &
setProccessNum
done
}
if [ $proccessNum -lt $needNum ]
then
echo "實際進程數爲:"$proccessNum"需要進程數爲:"$needNum"增加進程數..."
start_pro
elif [ $proccessNum -gt $needNum ]
then
echo "實際進程數爲:"$proccessNum"需要進程數爲:"$needNum"減少進程數..."
stop_pro
sleep 1
start_pro
fi
二.使用說明
- 將代碼寫入文件,路徑與項目需要自行調整
- 加入定時linux任務檢測