如何监控docker容器运行状态 shell 脚本

如何监控docker容器运行状态 shell 脚本,第1张

如何监控docker容器运行状态 shell 脚本 场景

公司项目部署在docker中,由于未知原因容器偶尔会停止,需要写一个脚本监控 容器运行状态,如果容器停止了,就再启动该容器

shell脚本
#!/bin/bash
# 传入容器名称
containerName=
currTime=`date +"%Y-%m-%d %H:%M:%S"`
# 查看进程是否存在
exist=`docker inspect --format '{{.State.Running}}' ${containerName}`
if [ "${exist}" != "true" ]; then
 docker start ${containerName}
 # 记录
 echo "${currTime} 重启docker容器,容器名称:${containerName}" >> /mnt/xvde1/ms_ctynyd/scripts/wbwf_monitor.log
 
fi
定时任务crontab

crontab -e 进行 linux 定时任务的编辑

# 监控docker 8081 容器
*/1 * * * * sh /mnt/xvde1/ms_ctynyd/scripts/wbwf_monitor.sh server_wbwf_wbwf-app_1
# 监控 docker 7081 容器
*/1 * * * * sh /mnt/xvde1/ms_ctynyd/scripts/wbwf_monitor.sh server_hb_hb-test-app_1

补充:shell脚本判断某服务是否运行

shell脚本判断某服务是否开启

脚本如下:
#!/bin/bash
#检查服务状态,是否安装
read -p "请输入要检测的服务:" SERVICE
netstat -anp | grep $SERVICE &> /dev/null
if [ $? -eq 0 ]
then
  echo "$SERVICE服务已经启动!"
else
  rpm -q $SERVICE &> /dev/null
 
  if [ $? -eq 0 ]
  then
 echo "$SERVICE服务已安装,正在启动...."
    service $SERVICE start
  else
 echo "该服务未安装!"
 fi
fi
结果如下:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/898451.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存