我有以下 ECS 容器定義
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
#!/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(),但是信號沒有被捕獲。
我登錄容器檢查行程
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 個容器,我認為信號只發送到第一個容器 - 當我移除第一個容器時,我能夠看到清理作業。任何想法如何處理這個?
uj5u.com熱心網友回復:
根據(加粗我的)的 POSIX 檔案:exec
如果 exec 指定為
command,它將替換 shellcommand而不創建新行程。...
trap一旦您呼叫exec "$@".
這可能會做你想要的,但它完全未經測驗,我還沒有分析"$@"你構造的字串:
.
.
.
task_cleanup() {
echo "Deleting files"
rm -rf /tmp/*
}
trap task_cleanup SIGTERM SIGINT
# background the task
"$@" &
pid=$!
wait $pid
kill $pid
exit
這不是用保存在 中的命令替換shell,而是"$@"將該命令作為 shell 的子行程運行。
uj5u.com熱心網友回復:
將以下腳本保存在test.sh
#!/usr/bin/env bash
task_cleanup() {
echo "Deleting files"
}
trap task_cleanup SIGTERM
"$@"
并運行它:
bash test.sh sleep 60
現在運行ps -ef|grep "bash test.sh sleep 60"(在另一個 bash 會話中)以獲取 PID,然后運行
kill -TERM PID
該行程將捕獲信號 TERM 但僅在完成task_cleanup后運行sleep 60。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/455996.html
標籤:重击 码头工人 dockerfile 亚马逊-ecs
上一篇:如何將gitupdate-index--assume-unchanged<file>與pathspec一起使用?
