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 access MySql through IP address

1. Log in to mysql: mysql -u root -h 127.0.0.1 -p...

Detailed explanation of the four transaction isolation levels in MySQL

The test environment of this experiment: Windows ...

vue $set implements assignment of values ​​to array collection objects

Vue $set array collection object assignment In th...

Use viewport in meta tag to define screen css

<meta name="viewport" content="w...

HTML hyperlink a tag_Powernode Java Academy

Anyone who has studied or used HTML should be fam...

Vue routing returns the operation method of restoring page status

Route parameters, route navigation guards: retain...

Why developers must understand database locks in detail

1.Lock? 1.1 What is a lock? The real meaning of a...

Detailed explanation of using scp command to copy files remotely in Linux

Preface scp is the abbreviation of secure copy. s...

How to implement animation transition effect on the front end

Table of contents Introduction Traditional transi...

Analysis of the reasons why Vue3 uses Proxy to implement data monitoring

Vue data two-way binding principle, but this meth...

Improvements to the web server to improve website performance

<br />In the first section of this series, w...

Summary of common Mysql DDL operations

Library Management Create a library create databa...

A complete tutorial on using axios encapsulation in vue

Preface Nowadays, in projects, the Axios library ...

How MySQL supports billions of traffic

Table of contents 1 Master-slave read-write separ...

Windows DNS server exposed "worm-level" vulnerability, has existed for 17 years

Vulnerability Introduction The SigRed vulnerabili...