File size: 767 Bytes
30db2e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env bash
# Run a pipeline command under a hard virtual-memory cap so a runaway job
# dies with an allocation error instead of OOM-killing the whole VM.
#
# Usage: scripts/run_capped.sh [command...]
# Env:   ETYMOLOGY_MEMLIMIT_GIB (default 10). Set to 0 to disable ulimit.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
export PYTHONPATH="${PYTHONPATH:-$ROOT}"
export PATH="${HOME}/.local/bin:${PATH}"

GIB="${ETYMOLOGY_MEMLIMIT_GIB:-10}"
if [[ "$GIB" != "0" ]]; then
  # ulimit -v is in KiB on Linux.
  KB="$(python3 -c "print(int(float('${GIB}') * 1024 * 1024))")"
  ulimit -v "$KB"
  echo "run_capped: ulimit -v ${KB} KiB (${GIB} GiB)" >&2
fi

if [[ $# -eq 0 ]]; then
  echo "usage: $0 command [args...]" >&2
  exit 2
fi
exec "$@"