2017-07-28 14:48:19 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# Starts the arguments as a bash command in background with low priorty. The
|
|
|
|
# command will be killed if it executes for over 1 hour. If the command failed
|
|
|
|
# to start, this script returns 127. If the command is timed out, this script
|
|
|
|
# returns 255. Otherwise the return value of the command will be returned.
|
|
|
|
|
2020-02-02 19:35:21 +00:00
|
|
|
echo "TIMEOUT in environment: ${TIMEOUT}"
|
2017-07-28 14:48:19 +00:00
|
|
|
|
2020-02-02 19:35:21 +00:00
|
|
|
if [ -z "${TIMEOUT}" ]; then
|
2017-07-28 14:48:19 +00:00
|
|
|
TIMEOUT=3600
|
|
|
|
fi
|
|
|
|
|
2020-02-02 19:35:21 +00:00
|
|
|
echo "Timeout has been set to ${TIMEOUT} seconds"
|
2017-07-28 14:48:19 +00:00
|
|
|
|
|
|
|
echo "Will start command $*"
|
|
|
|
|
|
|
|
echo "$@" | nice -n 19 sh &
|
|
|
|
JOB_ID=$!
|
2020-02-02 19:35:21 +00:00
|
|
|
echo "Job id: ${JOB_ID}"
|
2017-07-28 14:48:19 +00:00
|
|
|
|
2024-06-15 20:54:32 +00:00
|
|
|
for i in $(seq 1 1 "${TIMEOUT}"); do
|
|
|
|
if ps -p "${JOB_ID}" >/dev/null 2>&1; then
|
2017-07-28 14:48:19 +00:00
|
|
|
# Job is still running.
|
|
|
|
sleep 1
|
2020-02-02 19:35:21 +00:00
|
|
|
ROUND=$(printf "%s" "${i}" | tail -c 1)
|
|
|
|
if [ "${ROUND}" -eq "0" ]; then
|
|
|
|
echo "Job ${JOB_ID} is still running ... waited for ${i} seconds."
|
2017-07-28 14:48:19 +00:00
|
|
|
fi
|
|
|
|
else
|
2020-02-02 19:35:21 +00:00
|
|
|
wait ${JOB_ID}
|
2017-07-28 14:48:19 +00:00
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "Command $* has timed out"
|
|
|
|
|
2020-02-02 19:35:21 +00:00
|
|
|
kill -9 ${JOB_ID}
|
2017-07-28 14:48:19 +00:00
|
|
|
exit 255
|