You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置AWS ECS任务的entrypoint.sh以维持shell_plus交互会话

解决AWS ECS任务执行shell_plus后自动终止的问题

修改entrypoint.sh实现存活控制

把现有的entrypoint.sh替换为以下内容,它会监控shell_plus进程的活动状态,在有活跃连接时保持容器运行,无活动10分钟后自动终止:

#!/bin/bash

# 无活动超时时间(10分钟,单位:秒)
INACTIVITY_TIMEOUT=600
# 状态检查间隔(单位:秒)
CHECK_INTERVAL=10

# 初始化最后活动时间戳
last_active=$(date +%s)

while true; do
    # 检查是否有shell_plus进程在运行(避免匹配grep自身)
    if ps aux | grep -q "[p]ython manage.py shell_plus"; then
        # 重置最后活动时间
        last_active=$(date +%s)
    fi

    # 计算已无活动的时长
    current_time=$(date +%s)
    idle_duration=$((current_time - last_active))

    # 超时则退出容器
    if [ $idle_duration -ge $INACTIVITY_TIMEOUT ]; then
        echo "无活动超过10分钟,终止任务"
        exit 0
    fi

    sleep $CHECK_INTERVAL
done

关键逻辑说明

  • 脚本启动后进入无限循环,每10秒检查一次shell_plus进程状态
  • 检测到shell_plus运行时,重置无活动计时器
  • 当shell_plus退出后,开始倒计时10分钟,期间若重新启动shell_plus会重置计时器
  • 10分钟无任何shell_plus活动时,脚本主动退出,容器随之终止

注意事项

  • 确保脚本具备可执行权限:在Dockerfile中添加RUN chmod +x /path/to/entrypoint.sh(替换为实际路径)
  • 若基础镜像没有ps命令,需安装依赖包:
    • Alpine镜像:apk add --no-cache procps
    • Debian/Ubuntu镜像:apt-get update && apt-get install -y procps && rm -rf /var/lib/apt/lists/*

内容的提问来源于stack exchange,提问作者Cedric Holz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 17:40:19