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

场景

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

shell脚本

#!/bin/bash

# 传入容器名称
containerName=$1
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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章