• 在用的脚本
#使用crontab实现每日自动重启沙暴2服务器
#该脚本使用RconA2sAPi检测服务器是否有玩家在线 https://github.com/hundunzhidian/RconA2sAPiThis
#每隔6小时执行
#0 */6 * * * bash inss_no_player.sh

#screen名字
inssscreen=("inss27100" "inss27110" "inss27120" "inss27130" "inss27140" "inss27150" "inss27160" "inss27170" "inss27180" "inss27190" "inss27200")
#ip地址
inssIP="119.188.247.66"
#所有端口
inssPort=(27101 27111 27121 27131 27141 27151 27161 27171 27181 27191 27201)
bEmpty=()
#检查服务器里是否有人
for port in "${inssPort[@]}"; do
    if curl -i -k -H "Content-type: application/json" -X POST -d '{"ip":"'$inssIP':'$port'"}' http://119.188.247.66:9090/servers 2>&1 | grep -q -E "\"players\"\:\"0\/8\"|\"players\"\:\"0\/10\"|"访问超时"|\"players\"\:\"0\/32\""; then
    #此处curl结果如果有匹配到"players":"0/8" "players":"0/10" "访问超时" 则返回0表示没有玩家并标记bEmpty=true,否则返回1并标记bEmpty=false
    #$?为0则表示没有玩家
    #$?为1则表示有玩家    
        bEmpty+=(true)
    else
        bEmpty+=(false)
    fi
done
for((i = 0; i < ${#inssscreen[@]}; i++)); do
    #如果screen存在
    if screen -ls | grep -q ${inssscreen[$i]}; then
        if [ "${bEmpty[$i]}" = true ]; then
            bash /opt/server/inss/inss.sh ${inssscreen[$i]} stop
            echo $(date "+%Y-%m-%d %H:%M:%S") server ${inssscreen[$i]} valveapi json: `curl -i -k -H "Content-type: application/json" -X POST -d '{"ip":"'$inssIP':'${inssPort[$i]}'"}' http://119.188.247.66:9090/servers 2>&1 | awk 'END{print}'` >> /opt/server/inss/shell/logs/inss_no_player.log 2>&1
            echo $(date "+%Y-%m-%d %H:%M:%S") Shutdown ${inssscreen[$i]} ...  >> /opt/server/inss/shell/logs/inss_no_player.log 2>&1
            sleep 1
            inss_screen_pid=`ps -ef | grep "${inssPort[$i]}" | grep "SCREEN" | grep -v "grep" | grep -v "sissm" |awk '{print $2}'`
                if [[ ! -z "$inss_screen_pid" ]];then
                    echo $(date "+%Y-%m-%d %H:%M:%S") kill -9 $inss_screen_pid commands: `ps -ef | grep "${inssPort[$i]}" | grep "SCREEN" | grep -v "grep"| awk '{$1=$2=$3=$4=$5=$6=$7=""; print $0}'` >> /opt/server/inss/shell/logs/inss_no_player.log 2>&1
                    kill \-9 $inss_screen_pid
                    screen -wipe >/dev/null 2>&1
                fi
            inss_process_pid=`ps -ef | grep "${inssPort[$i]}" | grep -v "grep" | grep -v "SCREEN" | grep -v "sissm" |awk '{print $2}'`
                if [[ ! -z "$inss_process_pid" ]];then
                    echo $(date "+%Y-%m-%d %H:%M:%S") kill -9 $inss_process_pid commands: `ps -ef | grep "${inssPort[$i]}" | grep -v "grep" | grep -v "SCREEN" | awk '{$1=$2=$3=$4=$5=$6=$7=""; print $0}'` >> /opt/server/inss/shell/logs/inss_no_player.log 2>&1
                    kill \-9 $inss_process_pid
                fi
        fi
    fi
    #如果没有该screen则启动
    if ! screen -ls | grep -q ${inssscreen[$i]}; then
        #启动脚本
        bash /opt/server/inss/inss.sh ${inssscreen[$i]} start
        echo $(date "+%Y-%m-%d %H:%M:%S") Starting ${inssscreen[$i]} ...  >> /opt/server/inss/shell/logs/inss_no_player.log 2>&1
    fi
done

标签: none

添加新评论