How to monitor the running status of docker container shell script

How to monitor the running status of docker container shell script

Scenario

The company project is deployed in Docker. Due to unknown reasons, the container occasionally stops. It is necessary to write a script to monitor the running status of the container. If the container stops, start the container again.

Shell Script

#!/bin/bash
# Pass in the container name containerName=$1
currTime=`date +"%Y-%m-%d %H:%M:%S"`
# Check if the process exists=`docker inspect --format '{{.State.Running}}' ${containerName}`
if [ "${exist}" != "true" ]; then
 docker start ${containerName}
 # Record echo "${currTime} restart docker container, container name: ${containerName}" >> /mnt/xvde1/ms_ctynyd/scripts/wbwf_monitor.log
 
fi

Scheduled tasks crontab

crontab -e to edit Linux scheduled tasks

# Monitor docker 8081 container*/1 * * * * sh /mnt/xvde1/ms_ctynyd/scripts/wbwf_monitor.sh server_wbwf_wbwf-app_1
# Monitor docker 7081 container*/1 * * * * sh /mnt/xvde1/ms_ctynyd/scripts/wbwf_monitor.sh server_hb_hb-test-app_1

Supplement: Shell script determines whether a service is running

Shell script to determine whether a service is enabled

The script is as follows:

#!/bin/bash
#Check the service status to see if it is installed read -p "Please enter the service to be tested:" SERVICE
netstat -anp | grep $SERVICE &> /dev/null
if [ $? -eq 0 ]
then
  echo "$SERVICE service has been started!"
else
  rpm -q $SERVICE &> /dev/null
 
  if [ $? -eq 0 ]
  then
 echo "$SERVICE service has been installed and is starting...."
    service $SERVICE start
  else
 echo "The service is not installed!"
 fi
fi

The results are as follows:

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Docker enables seamless calling of shell commands between container and host
  • How to execute Linux shell commands in Docker
  • Detailed explanation of Shell script control docker container startup order
  • Execute the shell or program inside the Docker container on the host
  • Use Shell scripts to batch start and stop Docker services
  • Shell script builds Docker semi-automatic compilation, packaging and release application operations

<<:  Web development tutorial cross-domain solution detailed explanation

>>:  The difference between html block-level tags and inline tags

Recommend

How to deploy redis in linux environment and install it in docker

Installation Steps 1. Install Redis Download the ...

Summary of several common methods of JavaScript arrays

Table of contents 1. Introduction 2. filter() 3. ...

Implementation of CSS Fantastic Border Animation Effect

Today I was browsing the blog site - shoptalkshow...

MySQL master-slave principle and configuration details

MySQL master-slave configuration and principle, f...

How to set static IP for Ubuntu 18.04 Server

1. Background Netplan is a new command-line netwo...

Centos7 install mysql5.6.29 shell script

This article shares the shell script of mysql5.6....

How to install MySQL database on Debian 9 system

Preface Seeing the title, everyone should be thin...

Summary of some reasons why crontab scheduled tasks are not executed

Preface I recently encountered some problems at w...

Nginx routing forwarding and reverse proxy location configuration implementation

Three ways to configure Nginx The first method di...

JS+Canvas draws a lucky draw wheel

This article shares the specific code of JS+Canva...

Detailed explanation of special phenomena examples of sleep function in MySQL

Preface The sleep system function in MySQL has fe...

When the interviewer asked the difference between char and varchar in mysql

Table of contents Difference between char and var...

How to migrate the data directory in mysql8.0.20

The default storage directory of mysql is /var/li...