shell 监听docker 容器是否正常运行,并重启

  • 编写脚本
    vim /opt/listen/listen_container_mysql.sh
#!/bin/bash
#监控容器的运行状态
#容器名称  传入参数
containerName=$1
#当前时间
now=`date +"%Y-%m-%d %H:%M:%S"`
# 查看进程是否存在
exist=`docker inspect --format '{{.State.Running}}' ${containerName}`
if [ "${exist}" != "true" ]
then {
    echo "${now} 重启docker容器,容器名称:${containerName}" >> /opt/listen/logs/mysl.log
    docker start ${containerName}
}
else
{
    echo "${now} 容器名称:${containerName} 正常" >>/opt/listen/logs/mysl.log
}
fi
  • 试运行脚本,检查脚本是否能够正常启动,不报错
sh /opt/listen/listen_container_mysql.sh 6fb4c99476a0  
  • 加入定时任务
crontab -e

每个一分钟执行一次脚本

*/1 * * * * sh /opt/listen/listen_container_mysql.sh 6fb4c99476a0  >/dev/null 2>&1

重新载入

service crond reload
  • 安装crontab:
yum install crontabs

crontab服务操作说明:

service crond start //启动服务

service crond stop //关闭服务

service crond restart //重启服务

service crond reload //重新载入配置
service crond status // 查看状态
crontab -l //查询定时任务列表

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。