容器检测及自启动
简介
当容器意外退出时,可以根据脚本自启动
场景
目前还没有上 k8s, 当容器意外停止时(如容器里面的go服务抛出panic),服务就不能用了,可能会影响正常的业务,这时候,就需要有个监测的脚本,去检测容器是否正常,如果停止,需要启动它.
代码
#!/bin/bash
# 容器检测
#-----------------
# 需要检测的容器名
containerArr=(red-tomato-admin red-tomato-api red-tomato-business red-tomato-im)
# 循环
for containerName in ${containerArr[@]}; do
echo "---------------"
echo "容器名:${containerName}"
currTime=$(date +"%Y-%m-%d %H:%M:%S")
echo "检测时间:${currTime}"
# 查看进程是否存在
exist=$(docker inspect --format '{{.State.Running}}' ${containerName})
echo "状态:${exist}"
if [ "${exist}" != "true" ]; then
echo "执行 run.sh 脚本启动容器"
# docker start ${containerName}
/www/wwwroot/sh/${containerName}/run.sh
# 记录
# echo "${currTime} 重启docker容器,容器名称:${containerName}" >> /xxx/${containerName}.log
fi
done
#-----------------
再结合定时任务,定时去执行上面的脚本.这样就能保证服务正常了.