bash 在bash中捕获来自“docker stop”的信号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20602675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:00:39  来源:igfitidea点击:

trapping signal from "docker stop" in bash

bashdocker

提问by ben schwartz

i have an entry point script in a docker container that looks something like the following:

我在 docker 容器中有一个入口点脚本,如下所示:

#!/bin/bash

echo starting up

function shut_down() {
    echo shutting down

    pid=$(ps -e | grep myapp | awk '{print }')
    kill -SIGTERM $pid  
    exit
}

trap "shut_down" SIGKILL SIGTERM SIGHUP SIGINT EXIT

/opt/myapp

I can't figure out how to trap the signal sent in by running docker stopon the container. When running interactively, a ctrl+cwill trigger it as expected, but a docker stopcommand just waits for the 10 second timeout, and exits without ever entering the shut_downfunction

我不知道如何通过docker stop在容器上运行来捕获发送的信号。交互式运行时,actrl+c会按预期触发它,但docker stop命令只是等待 10 秒超时,然后退出而从未进入该shut_down函数

How can I trap the signal sent by docker stopin bash to do some cleanup?

如何捕获docker stopbash 中发送的信号以进行一些清理?

采纳答案by Luca G. Soave

Have a look at this, could be inspiring :-)

看看这个,可能会鼓舞人心:-)

UPDATE:

更新:

@nick-humrich here it is is my lamer copy & past (credit goes to the original author https://github.com/lgierth)

@nick-humrich 这是我的 lamer 副本和过去(归功于原作者https://github.com/lgierth

#!/bin/bash

function ensure_started_container {
  exists=`docker ps -q | grep `
  if [ "$?" = "0" ] ; then
    echo "[docker-exec] skipping docker start, already started"
  else
    output=`docker start ""`
    echo "[docker start] $output"
  fi
  running=1
}

function setup_signals {
  cid=""; shift
  handler=""; shift
  for sig; do
    trap "$handler '$cid' '$sig'" "$sig"
  done
}

function handle_signal {
  echo "[docker-exec] received "
  case "" in
    SIGINT)
      output=`docker stop -t 5 ""`
      echo "[docker stop] $output"
      running=0
      ;;
    SIGTERM)
      output=`docker stop -t 5 ""`
      echo "[docker stop] $output"
      running=0
      ;;
    SIGHUP)
      output=`docker restart -t 5 ""`
      echo "[docker restart] $output"

      # restart logging
      docker attach "" &
      kill "$logger_pid" 2> /dev/null
      logger_pid="$!"
      ;;
  esac
}

running=0

setup_signals "" "handle_signal" SIGINT SIGTERM SIGHUP

ensure_started_container ""

docker attach "" &
logger_pid="$!"

while true; do
  if [ "$running" = "1" ]; then
    sleep 1
  else
    break
  fi
done

exit_code=`docker wait ""`
exit "$exit_code"

回答by Alen Komljen

Old question, but this will do the trick:

老问题,但这可以解决问题:

#!/bin/bash

echo starting up

exec /opt/myapp

For details take a look here: https://akomljen.com/stopping-docker-containers-gracefully/

详情请看这里:https: //akomljen.com/stopping-docker-containers-gracefully/

回答by Maxime

Since Docker 1.9, you can use the STOPSIGNALinstruction in your Dockerfile:

从 Docker 1.9 开始,您可以使用以下STOPSIGNAL指令Dockerfile

STOPSIGNAL signal

The STOPSIGNALsets the system call signal that will be sent to the container to exit. This signal can be a valid unsigned number that matches a position in the kernel's syscall table, for instance 9, or a signal name in the format SIGNAME, for instance SIGKILL.

STOPSIGNAL signal

STOPSIGNAL设置将被发送到容器退出系统呼叫信号。该信号可以是与内核系统调用表中的位置匹配的有效无符号数,例如 9,或格式为 SIGNAME 的信号名称,例如 SIGKILL。

Source: https://docs.docker.com/engine/reference/builder/#stopsignal

来源:https: //docs.docker.com/engine/reference/builder/#stopsignal