一.问题与思考
项目中使用IM中间件rabbitMQ,存在常驻进程异常退出,需要自动监测项目中所有的worker正常运行(异常退出worker监测与拉起).
二.流程处理
- 建立项目worker配置文件
- linux定时检测worker状态
- 当前worker状态获取
- 对于实际worker数据与配置不符合的,重置该业务worker进程数量
三.主要代码实现
- 项目worker配置
[common_test]
cmd = common/test/index
test_procs = 2
prod_procs = 5
[msg_test]
cmd = msg/test/index
test_procs = 2
prod_procs = 5
- 当前worker状态获取及配置worker数量信息
<?php
/**
* Created by PhpStorm.
* User: 小小徐
* Date: 2019/8/25
* Time: 16:38
*/
$arr = parse_ini_file(__DIR__.'/../daemon2.ini',true);
if(getenv('APP_ENV') != 'prod'){
$needNumKey = "test_procs";
}else{
$needNumKey = "prod_procs";
}
foreach ($arr as $key=>$value)
{
$cmd = $value['cmd'];
$execNum = exec("ps aux|grep -v grep|grep $cmd|wc -l");
echo $key.' '.$cmd.' '.$execNum.'/'.$value[$needNumKey].PHP_EOL;
}
- 检测当前worker数量是否和配置数量一致,不一致时修复worker进程
#!/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`
proStatusSaveFile="/var/www/html/proStatus.log"
proExecFile=`readlink -f $executePath/parseIni.php`
proccessName=
#declare -i proccessNum
#declare -i needNum
proccessNum=0
needNum=0
function setProccessNum
{
proccessNum=`ps aux|grep -v grep|grep $proccessName|wc -l`
}
function stop_pro()
{
setProccessNum
pids=$(ps aux|grep -v grep|grep ${proccessName}|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
}
function reset()
{
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
}
function checkType(){
local a="$1"
printf "%d" "$a" &>/dev/null && echo "integer" && return
printf "%d" "$(echo $a|sed 's/^[+-]\?0\+//')" &>/dev/null && echo "integer" && return
printf "%f" "$a" &>/dev/null && echo "number" && return
[ ${#a} -eq 1 ] && echo "char" && return
echo "string"
}
function proccessInit()
{
proccessName=$2
arr=($(echo $3 | awk 'BEGIN{FS="/";OFS=" "} {print strtonum($1),strtonum($2)}'))
proccessNum=${arr[0]}
needNum=${arr[1]}
setProccessNum
}
php $proExecFile > $proStatusSaveFile
while read line
do
proccessInit $line
reset
done < $proStatusSaveFile
四.遇到的问题与实现
php在计算的时候会自动将字符串类型的变量转化为数值类型,shell使用字符串实现运算时(判断大于小于)会报错!通过百度通过awk与strtonum解决该问题