首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未被AWS ECS码头集装箱捕获

未被AWS ECS码头集装箱捕获
EN

Stack Overflow用户
提问于 2022-04-01 23:27:53
回答 2查看 756关注 0票数 0

我有以下ECS容器定义

代码语言:javascript
复制
ContainerDefinitions:
        - Name: myfluentd
          Essential: true
          Image: !Ref DockerImage
          MountPoints:
            - SourceVolume: efs-volume
              ContainerPath: '/fluentd/buffers'
              ReadOnly: false
          PortMappings:
            - ContainerPort: 24224
              HostPort: 24224
              Protocol: tcp
          EntryPoint:
            - "/var/lib/twistlock/defender.sh"
            - "defender"
            - "entrypoint"
            - "tini"
            - "--"
            - "/bin/entrypoint.sh"
          Command:
            - fluentd
          VolumesFrom:
            - ReadOnly: false
              SourceContainer: "TwistlockDefender"

ENTRYPOINT

["/var/lib/twistlock/fargate/fargate_defender.sh","fargate","entrypoint","tini","--","/bin/entrypoint.sh"]

以及entrypoint.sh的含量

代码语言:javascript
复制
#!/bin/sh

#source vars if file exists
DEFAULT=/etc/default/fluentd

if [ -r $DEFAULT ]; then
    set -o allexport
    . $DEFAULT
    set +o allexport
fi

# If the user has supplied only arguments append them to `fluentd` command
if [ "${1#-}" != "$1" ]; then
    set -- fluentd "$@"
fi

# If user does not supply config file or plugins, use the default
if [ "$1" = "fluentd" ]; then
    if ! echo $@ | grep ' \-c' ; then
       set -- "$@" -c /fluentd/etc/${FLUENTD_CONF}
    fi

    if ! echo $@ | grep ' \-p' ; then
       set -- "$@" -p /fluentd/plugins
    fi
fi

. /bin/env.sh

task_cleanup() {
    echo "Deleting files"
    rm -rf /tmp/*
    exit
}
trap task_cleanup SIGTERM SIGINT

exec "$@"

当我停止ECS任务时,它会向容器发出SIGTERM,并期望它执行task_cleanup(),但是信号没有被捕获。

我登录到容器以检查进程。

代码语言:javascript
复制
PID   USER     TIME  COMMAND
    1 fluent    0:00 sh /var/lib/twistlock/defender.sh defender entrypoint tini -- /bin/entrypoint.sh fluentd
    7 fluent    0:00 /var/lib/twistlock/defender defender entrypoint tini -- /bin/entrypoint.sh fluentd
   16 root      0:00 /managed-agents/execute-command/amazon-ssm-agent
   22 fluent    0:00 /sbin/tini -- /bin/entrypoint.sh fluentd
   29 fluent    0:02 {fluentd} /usr/bin/ruby /usr/bin/fluentd -c /fluentd/etc/fluent.conf -p /fluentd/plugins
   72 root      0:00 /managed-agents/execute-command/ssm-agent-worker
   90 fluent    1:06 /usr/bin/ruby -Eascii-8bit:ascii-8bit /usr/bin/fluentd -c /fluentd/etc/fluent.conf -p /fluentd/plugins --under-supervisor
  546 root      0:00 /managed-agents/execute-command/ssm-session-worker ecs-execute-command-03931001c6df08625
  556 root      0:00 /bin/bash
  870 root      0:00 ps aux 

我遗漏了什么?为什么信号没有被困住?

更新:ENTRYPOINT启动了2个容器,我认为信号只发送到第一个容器--当我移除第一个容器时,我能够看到清理工作。有什么好办法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-01 23:52:52

Per the POSIX documentation for exec (黑石矿):

如果exec是用command指定的,那么它应该在不创建新进程的情况下将

替换为。..。

您的shell进程和您使用trap创建的信号处理程序在调用exec "$@"之后就不再存在了。

这可能会做您想做的事情,但是它是完全未经测试的,我还没有分析您构建的字符串"$@"

代码语言:javascript
复制
   .
   .
   .
task_cleanup() {
    echo "Deleting files"
    rm -rf /tmp/*
}
trap task_cleanup SIGTERM SIGINT

# background the task
"$@" &
pid=$!

wait $pid

kill $pid

exit

该命令将作为shell的子进程运行,而不是用"$@"中持有的命令替换shell。

票数 2
EN

Stack Overflow用户

发布于 2022-04-02 10:37:02

test.sh中保存以下脚本

代码语言:javascript
复制
#!/usr/bin/env bash
  
task_cleanup() {
    echo "Deleting files"
}
trap task_cleanup SIGTERM

"$@"

然后用以下方式运行:

代码语言:javascript
复制
bash test.sh sleep 60

现在运行ps -ef|grep "bash test.sh sleep 60" (在另一个bash会话中)以获取PID,然后运行

代码语言:javascript
复制
kill -TERM PID

该进程将捕获信号项,但只在task_cleanup完成后运行sleep 60

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71713652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档