一个简单的容器检测脚本

臭大佬 2024-02-16 14:21:27 672
Docker 
简介 一个简单的容器检测脚本

代码

imTimer.sh:定时器检测

#!/bin/bash

while true
do
    # 检测脚本
    restart.sh
    sleep 5
done

restart.sh:检测和重启代码

#!/bin/bash

env="prod"
httpUrl="http://127.0.0.1:3200/system/state"
# 项目
objName="im"
# 设置镜像名
imagesName="${objName}"

containerName=${objName}
# 当前时间
currentTime=$(date +"%Y%m%d%H%M%S")

# 检测容器是否可以访问
response=$(curl --write-out %{http_code} --silent --output /dev/null ${httpUrl})
# 查看进程是否存在
exist=$(docker inspect --format '{{.State.Running}}' ${containerName})
echo "时间 ${currentTime}"
echo "容器 ${containerName} 状态:${exist}"
echo "地址 ${httpUrl}  状态:${response}"
if [ "${exist}" != "true" ] || [ "$response" -ne 200 ]; then
echo "容器 ${containerName} 执行重启"
docker start ${containerName}

logFile=${containerName}-"${currentTime}".log
echo "写入 ${logFile} 日志"
 docker logs -f ${containerName} > /wwwlogs/${objName}/temp/${logFile} &

fi

执行

nohup imTimer.sh &

查看和杀死进程

pgrep -f imTimer.sh
 kill <PID>
查看占用高的进程
 top
查看是哪个程序
 ps -ef | grep <PID>

上一篇: 制作一个简易的exe包

下一篇: pprof介绍