what's important: * if task code ran, it exits with 0. this code is regardless of (error, result) * when it exited cleanly, we will get the values from the database * if task timed out, the box code kills it and it has a flag tracking timedOut. we can ignore exit code in this case. * if task code was stopped, box code will send SIGTERM which ideally it will handle and end with 70. * if task code crashed and it caught the exception, it will return 50 * if task code crashed and node nuked us, it will exit with 1 * if task code was killed with some unhandleabe signal, taskworker.sh will return the signal (9=SIGKILL)
49 lines
1.1 KiB
Bash
Executable File
49 lines
1.1 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
set -eu -o pipefail
|
|
|
|
if [[ ${EUID} -ne 0 ]]; then
|
|
echo "This script should be run as root." > /dev/stderr
|
|
exit 1
|
|
fi
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
echo "No arguments supplied"
|
|
exit 1
|
|
fi
|
|
|
|
if [[ "$1" == "--check" ]]; then
|
|
echo "OK"
|
|
exit 0
|
|
fi
|
|
|
|
function killtree() {
|
|
local pid=$1
|
|
for cpid in $(pgrep -P "$pid"); do
|
|
killtree "${cpid}" || true
|
|
done
|
|
echo "kill-child: killing $pid"
|
|
kill -SIGTERM "${pid}" 2>/dev/null || true
|
|
sleep 1
|
|
kill -SIGKILL "${pid}" 2>/dev/null || true
|
|
}
|
|
|
|
readonly target_pid="$1"
|
|
readonly expected_parent_pid="$2"
|
|
|
|
readonly target_actual_parent_pid=$(ps -o ppid= -p "${target_pid}" 2>/dev/null | tr -d ' ')
|
|
|
|
if [[ -z "${target_actual_parent_pid}" ]]; then
|
|
echo "kill-child: target PID ${target_pid} does not exist."
|
|
exit 1
|
|
fi
|
|
|
|
if [[ "${target_actual_parent_pid}" -ne "${expected_parent_pid}" ]]; then
|
|
echo "kill-child: refusing to kill — PID ${target_pid} is not a child of ${expected_parent_pid}."
|
|
exit 1
|
|
fi
|
|
|
|
readonly child_cmd=$(ps -o cmd= -p "${target_pid}")
|
|
echo "kill-child: kill PID ${target_pid} (command: ${child_cmd})"
|
|
killtree ${target_pid}
|