From 4baa1542ca6a611649d02249c09796db826d8693 Mon Sep 17 00:00:00 2001 From: xxm Date: Wed, 5 Aug 2026 15:07:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20heavy=5Frun.sh=20=E9=87=8D=E5=9E=8B?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=8F=97=E6=8E=A7=E8=BF=90=E8=A1=8C=E5=99=A8?= =?UTF-8?q?(=E9=98=B2=E8=B5=84=E6=BA=90=E4=BA=8B=E6=95=85)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - flock单重型任务锁(同时只许一个) - nice19+ionice最低档(让位生产服务) - systemd-run MemoryMax6G/CPUQuota150%(防单进程吃光) - load1>4拒绝启动(负载预检) 铁律: 246是生产命脉, 研究型重负载必须经此脚本运行 --- deploy/profile-scripts/heavy_run.sh | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 deploy/profile-scripts/heavy_run.sh diff --git a/deploy/profile-scripts/heavy_run.sh b/deploy/profile-scripts/heavy_run.sh new file mode 100755 index 00000000..5cd2f674 --- /dev/null +++ b/deploy/profile-scripts/heavy_run.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# heavy_run.sh — 研究型重负载受控运行器(2026-08-05 进程堆积/资源事故后立) +# +# 铁律:246 是生产命脉(frp/gitea/agentsmeeting/MoFin 都在上面)。 +# 一切研究型重负载(全市场回测/批量采集/大规模分析)必须通过本脚本运行: +# 1. flock 单重型任务锁 —— 同时只许一个重型任务存在 +# 2. nice -19 + ionice 最低档 —— CPU/IO 让位于生产服务 +# 3. systemd-run 内存/CPU 配额 —— MemoryMax 6G / CPUQuota 150%(防单进程吃光资源) +# 4. 负载预检 —— load1 > 4 时拒绝启动(等闲了再说) +# +# 用法: nohup /home/hmo/MoFin/deploy/profile-scripts/heavy_run.sh /usr/bin/python3 /tmp/xxx.py > /tmp/xxx.log 2>&1 & +LOCK=/tmp/heavy_run.lock +LOAD_LIMIT=4 + +load1=$(awk '{print $1}' /proc/loadavg) +if awk -v l="$load1" -v lim="$LOAD_LIMIT" 'BEGIN{exit !(l>lim)}'; then + echo "[heavy_run] 当前负载 $load1 > $LOAD_LIMIT,拒绝启动重型任务(先等服务器闲下来)" >&2 + exit 1 +fi + +exec 9>"$LOCK" +if ! flock -n 9; then + pid=$(fuser "$LOCK" 2>/dev/null) + echo "[heavy_run] 已有重型任务在跑(lock: $LOCK),拒绝并发启动" >&2 + exit 1 +fi + +echo "[heavy_run] 受控启动: $* (nice19/ionice低/MemoryMax6G/CPUQuota150%)" >&2 +systemd-run --scope --quiet -p MemoryMax=6G -p CPUQuota=150% --nice=19 ionice -c2 -n7 "$@" +rc=$? +echo "[heavy_run] 任务结束 rc=$rc" >&2 +exit $rc