timeout/timeout

173 lines
3.7 KiB
Plaintext
Raw Normal View History

2022-12-31 13:25:07 +01:00
#!/bin/sh
usage() {
2023-05-21 21:02:15 +02:00
printf "Usage: %s [-l DEBUG_LEVEL] [-v] [-h] [-t TIMEOUT] [-c COMMAND]\n\n" "$0"
2023-01-02 08:42:29 +01:00
}
help() {
cat << EOH
SYNOPSIS
2023-05-21 21:02:15 +02:00
$0 [-l DEBUG_LEVEL] [-v] [-h] [-t TIMEOUT] [-c COMMAND]
2023-01-02 08:42:29 +01:00
DESCRIPTION
\`Timeout\` executes a command until the timeout is over.
When the command is finished before the timeout then the exit code is \`0\` else it's \`1\`.
OPTIONS
-h Show this help
2023-05-21 21:02:15 +02:00
-l debug|info|notice|warning|error
Debug level
2023-01-02 08:42:29 +01:00
-v Display the command output (stderr)
-c COMMAND
The command to execute
\`COMMAND=... $0\` works to.
-t TIMEOUT
The timeout in seconds (default: 10)
\`TIMEOUT=... $0\` works to.
EOH
2022-12-31 13:25:07 +01:00
}
on_interrupt() {
2023-05-21 21:02:15 +02:00
log -l notice "Process aborted!"
2022-12-31 13:25:07 +01:00
2023-01-02 08:42:29 +01:00
exit 130
2022-12-31 13:25:07 +01:00
}
create_script() {
2023-01-02 08:42:29 +01:00
printf "#!/bin/sh\n%s\n" "$1" > "$2"
chmod +x "$SCRIPT_FILE"
2023-05-21 21:02:15 +02:00
log -t -l debug "$SCRIPT_FILE created"
2022-12-31 13:25:07 +01:00
}
run_script() {
2023-01-02 08:42:29 +01:00
if [ "$2" -eq 0 ]; then
"$SCRIPT_FILE" >/dev/null 2>&1 &
else
2023-05-21 21:02:15 +02:00
"$SCRIPT_FILE" 2>"$LOG_FILE" >&2 &
2023-01-02 08:42:29 +01:00
fi
2022-12-31 13:25:07 +01:00
2023-05-21 21:02:15 +02:00
log -t -l debug "$SCRIPT_FILE started"
2023-01-02 08:42:29 +01:00
printf "%d" $!
2022-12-31 13:25:07 +01:00
}
stop_delete_script() {
2023-05-21 21:02:15 +02:00
(
kill -9 "$1" 2>/dev/null \
&& log -t -l debug "Process \"$1\" killed"
) || log -t -l debug "Process \"$1\" does not exist"
test -f "$2" && rm -f "$2"
test -f "$3" && rm -f "$3"
2022-12-31 13:25:07 +01:00
}
check_pid() {
2023-01-02 08:42:29 +01:00
PID=$1
LOOP=$2
2022-12-31 13:25:07 +01:00
2023-01-02 08:42:29 +01:00
for _ in $(seq 1 "$((LOOP*2))"); do
test -d "/proc/$PID/" && sleep 0.5 || STATUS=0
done
2022-12-31 13:25:07 +01:00
2023-01-02 08:42:29 +01:00
printf "%d" ${STATUS:-1}
2022-12-31 13:25:07 +01:00
}
main() {
2023-05-21 21:02:15 +02:00
while getopts "l:ht:c:v" option; do
2023-01-02 08:42:29 +01:00
case "${option}" in
h) help; exit 0;;
t) TIMEOUT="$OPTARG";;
c) COMMAND="$OPTARG";;
2023-05-21 21:02:15 +02:00
l) LOG_VERBOSE="$OPTARG";;
2023-01-02 08:42:29 +01:00
v) VERBOSE=1;;
2023-05-21 21:02:15 +02:00
?) log -l error "$(usage)"; exit 1;;
2023-01-02 08:42:29 +01:00
esac
done
2022-12-31 13:25:07 +01:00
2023-01-02 08:42:29 +01:00
TIMEOUT="${TIMEOUT:-10}"
VERBOSE="${VERBOSE:-0}"
2022-12-31 13:25:07 +01:00
2023-01-02 08:42:29 +01:00
if [ -z "$COMMAND" ]; then
2023-05-21 21:02:15 +02:00
log -l error "Command is required!"
2023-01-02 08:42:29 +01:00
exit 1
fi
2022-12-31 13:25:07 +01:00
2023-01-02 08:42:29 +01:00
SCRIPT_FILE="$(mktemp)"
2023-05-21 21:02:15 +02:00
LOG_FILE="$(mktemp)"
2022-12-31 13:25:07 +01:00
2023-01-02 08:42:29 +01:00
create_script "$COMMAND" "$SCRIPT_FILE"
2022-12-31 13:25:07 +01:00
2023-01-02 08:42:29 +01:00
SCRIPT_PID=$(run_script "$SCRIPT_FILE" "$VERBOSE")
2023-05-21 21:02:15 +02:00
(
test -f "$LOG_FILE" && tail -f "$LOG_FILE" | while read -r OUTPUT; do
LOG_VERBOSE=info log -t -l info "$OUTPUT"
done
)&
2023-01-02 08:42:29 +01:00
EXIT_STATUS=$(check_pid "$SCRIPT_PID" "$TIMEOUT")
2022-12-31 13:25:07 +01:00
2023-05-21 21:02:15 +02:00
stop_delete_script "$SCRIPT_PID" "$SCRIPT_FILE" "$LOG_FILE"
log -t -l debug "Exit code is $EXIT_STATUS"
2022-12-31 13:25:07 +01:00
2023-01-02 08:42:29 +01:00
exit "$EXIT_STATUS"
2022-12-31 13:25:07 +01:00
}
2023-05-21 21:02:15 +02:00
log() {
LOG_VERBOSE="${LOG_VERBOSE:-info}"
LEVEL=info
TIME=
while getopts "tl:" option; do
case "${option}" in
l) LEVEL="$OPTARG"; shift $((OPTIND-1));;
t) TIME="$(printf "[%s] " "$(date +'%Y-%m-%dT%H:%M:%S.%s')")"; shift $((OPTIND-1));;
*) exit 1;;
esac
done
if [ -t 2 ] && [ -z "${NO_COLOR-}" ]; then
case "${LEVEL}" in
debug) COLOR="$(tput setaf 3)";;
notice) COLOR="$(tput setaf 4)";;
warning) COLOR="$(tput setaf 5)";;
error) COLOR="$(tput setaf 1)";;
*) COLOR="$(tput sgr0)";;
esac
fi
case "${LEVEL}" in
debug) LEVEL=100;;
notice) LEVEL=250;;
warning) LEVEL=300;;
error) LEVEL=400;;
*) LEVEL=200;;
esac
case "${LOG_VERBOSE}" in
debug) LOG_VERBOSE_VALUE=100;;
notice) LOG_VERBOSE_VALUE=250;;
warning) LOG_VERBOSE_VALUE=300;;
error) LOG_VERBOSE_VALUE=400;;
*) LOG_VERBOSE_VALUE=200;;
esac
if [ $LEVEL -ge $LOG_VERBOSE_VALUE ]; then
printf "%s\n" "$*" | while IFS='' read -r LINE; do
printf "%s%s%s\n" "${COLOR:-}" "${TIME:-}" "$LINE" >&2
done
fi
}
2022-12-31 13:25:07 +01:00
trap on_interrupt INT
main "$@"