woj-server/resource/runner/scripts/run_timeout.sh

20 lines
475 B
Bash
Raw Normal View History

2022-10-20 18:21:11 +08:00
#!/usr/bin/env bash
. common.sh
function docker_run() {
local timeout=${TIMEOUT:-10}
2022-10-28 21:57:28 +08:00
local log_file=${LOG_FILE:-"/dev/stderr"}
local log_limit=${LOG_LIMIT:-4K}
2022-10-28 21:57:28 +08:00
log_info "$DOCKER run with timeout $timeout"
2022-10-20 18:21:11 +08:00
CONTAINER_NAME=$(uuidgen)
(
sleep "$timeout"
2022-10-28 21:57:28 +08:00
$DOCKER kill "$CONTAINER_NAME"
2022-10-20 18:21:11 +08:00
) &
2022-10-28 21:57:28 +08:00
$DOCKER run --rm --name "$CONTAINER_NAME" "$@" 2>&1 | head -c "$log_limit" >"$log_file"
2022-10-20 18:21:11 +08:00
pkill -P $$
2022-10-28 21:57:28 +08:00
$DOCKER kill "$CONTAINER_NAME" >/dev/null 2>&1
return 0
2022-10-20 18:21:11 +08:00
}