我有以下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个容器,我认为信号只发送到第一个容器--当我移除第一个容器时,我能够看到清理工作。有什么好办法吗?
发布于 2022-04-01 23:52:52
Per the POSIX documentation for exec (黑石矿):
如果exec是用command指定的,那么它应该在不创建新进程的情况下将
替换为。..。
您的shell进程和您使用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。
发布于 2022-04-02 10:37:02
在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该进程将捕获信号项,但只在task_cleanup完成后运行sleep 60。
https://stackoverflow.com/questions/71713652
复制相似问题