Files
SceneMarshal/scripts/media-rename
T
ali 97a71d5789 feat(wrapper): SSH 会话断开时结束容器内进程,避免孤儿继续跑
- media-rename 原来是 exec docker exec,客户端断开后容器进程不会退出
  (实测关窗口后 python 仍跑 200+ 秒;本次还清了 5 个历史孤儿,最长 56 小时)
- 改为前台运行 + trap HUP/INT/TERM,会话断开时按「main.py -p <目标路径>」
  扫 /proc 找进程并 kill -TERM
- 镜像里没有 pkill/pgrep,只能用 sh + tr 读 /proc/<pid>/cmdline 匹配
- 无明确目标时不做任何清理,避免误杀并发的其他任务
- 实测:强杀本地 ssh 后 5 秒内目标进程消失、无残留;正常流程退出码不变
2026-11-19 21:05:06 +08:00

107 lines
3.2 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# media-rename —— 宿主机代理,把参数透传进 python 容器执行 SceneMarshal
#
# 用法:
# media-rename # 处理当前目录
# media-rename auto # 指定 main.py 模式
# media-rename -p "/hdd/sda1/Quark-Down/苍兰诀 (2022)"
# media-rename -p 某目录 extract-subs
# media-rename -h # 透传 main.py 帮助
#
# 说明:
# - 未给 -p/--path 且不是 -h 时,默认取当前目录;
# - 路径会 realpath 展开,并限制在容器已挂载的范围内;
# - 有 TTY 时使用 docker exec -it(可交互确认/输入标题年份),无 TTY 时自动降级。
set -euo pipefail
CONTAINER="${MEDIA_RENAME_CONTAINER:-python}"
PYTHON_BIN="/python/media/bin/python"
SCRIPT="/python/media/src/main.py"
ALLOWED_PREFIXES=(/hdd/sda1 /python)
die() { printf '\033[0;31m❌ %s\033[0m\n' "$*" >&2; exit 1; }
args=()
path_found=0
target_path=""
want_help=0
i=1
while (( i <= $# )); do
arg="${!i}"
case "$arg" in
-h|--help)
want_help=1
args+=("$arg")
;;
-p|--path)
args+=("$arg")
path_found=1
i=$((i + 1))
(( i <= $# )) || die "$arg 后面缺少路径参数"
raw="${!i}"
target_path="$(realpath -e -- "$raw" 2>/dev/null)" || die "路径不存在或无法解析: $raw"
args+=("$target_path")
;;
*)
args+=("$arg")
;;
esac
i=$((i + 1))
done
if (( ! path_found && ! want_help )); then
target_path="$(pwd)"
args+=("-p" "$target_path")
fi
if [[ -n "$target_path" ]]; then
allowed=0
for prefix in "${ALLOWED_PREFIXES[@]}"; do
if [[ "$target_path" == "$prefix" || "$target_path" == "$prefix"/* ]]; then
allowed=1
break
fi
done
(( allowed )) || die "路径越界: $target_path 未挂载给容器(仅允许 ${ALLOWED_PREFIXES[*]})"
fi
exec_opts=(-u 1000:1000)
if [[ -t 0 && -t 1 ]]; then
exec_opts+=(-it)
fi
# docker exec 的客户端一旦断开,容器里的进程不会自动退出(会变孤儿继续跑)。
# 所以这里自己兜底:SSH 会话被挂断(SIGHUP)或收到 TERM/INT 时,
# 顺手把容器内同一目标的进程结束掉。
cleanup_container() {
# 容器是精简镜像,没有 pkill/pgrep,只能扫 /proc 自己找
[[ -n "$target_path" ]] || return 0 # 没有明确目标就不乱杀
pattern="$SCRIPT -p $target_path"
docker exec -u 1000:1000 "$CONTAINER" sh -c '
me=$$
pat="$1"
for p in /proc/[0-9]*; do
pid=${p#/proc/}
[ "$pid" = "$me" ] && continue
[ -r "$p/cmdline" ] || continue
c=$(tr "\0" " " < "$p/cmdline" 2>/dev/null) || continue
case "$c" in
*"$pat"*) kill -TERM "$pid" 2>/dev/null || true ;;
esac
done
' sh "$pattern" >/dev/null 2>&1 || true
}
trap 'cleanup_container; exit 129' HUP
trap 'cleanup_container; exit 130' INT
trap 'cleanup_container; exit 143' TERM
set +e
docker exec "${exec_opts[@]}" "$CONTAINER" "$PYTHON_BIN" "$SCRIPT" "${args[@]}"
code=$?
set -e
trap - HUP INT TERM
exit "$code"