1
1
mirror of https://github.com/mgree/ffs.git synced 2024-09-11 19:17:40 +03:00
ffs/utils/timeout
Michael Greenberg 6ed74ee0c7
Timing support (#46)
Benchmarks, in two flavors: real-world benchmarks and synthetic microbenchmarks.

`--time` flag for benchmarking output on stderr.

Using R to generate pretty graphs. Some overhaul of build scripts and artifacts, with the hope of simplifying the release system.
2021-07-29 17:55:53 -07:00

104 lines
2.8 KiB
Bash
Executable File

#!/bin/sh
# based on timeout3 from http://www.bashcookbook.com/bashinfo/source/bash-4.0/examples/scripts/timeout3
#
# The Bash shell script executes a command with a time-out.
# Upon time-out expiration SIGTERM (15) is sent to the process. If the signal
# is blocked, then the subsequent SIGKILL (9) terminates it.
#
# Based on the Bash documentation example.
# Hello Chet,
# please find attached a "little easier" :-) to comprehend
# time-out example. If you find it suitable, feel free to include
# anywhere: the very same logic as in the original examples/scripts, a
# little more transparent implementation to my taste.
#
# Dmitry V Golovashkin <Dmitry.Golovashkin@sas.com>
scriptName="${0##*/}"
DEFAULT_TIMEOUT=9
DEFAULT_INTERVAL=1
DEFAULT_DELAY=1
# Timeout.
timeout=${DEFAULT_TIMEOUT}
# Interval between checks if the process is still alive.
interval=${DEFAULT_INTERVAL}
# Delay between posting the SIGTERM signal and destroying the process by SIGKILL.
delay=${DEFAULT_DELAY}
printUsage() {
cat <<EOF
Synopsis
$scriptName [-t timeout] [-i interval] [-d delay] command
Execute a command with a time-out.
Upon time-out expiration SIGTERM (15) is sent to the process. If SIGTERM
signal is blocked, then the subsequent SIGKILL (9) terminates it.
-t timeout
Number of seconds to wait for command completion.
Default value: $DEFAULT_TIMEOUT seconds.
-i interval
Interval between checks if the process is still alive.
Positive integer, default value: $DEFAULT_INTERVAL seconds.
-d delay
Delay between posting the SIGTERM signal and destroying the
process by SIGKILL. Default value: $DEFAULT_DELAY seconds.
-l log
If a timeout occurs, the file log will be created.
As of today, Bash does not support floating point arithmetic (sleep does),
therefore all delay/time values must be integers.
EOF
}
# Options.
while getopts ":t:i:d:l:" option; do
case "$option" in
t) timeout=$OPTARG ;;
i) interval=$OPTARG ;;
d) delay=$OPTARG ;;
l) log=$OPTARG ;;
*) printUsage; exit 1 ;;
esac
done
shift $((OPTIND - 1))
# $# should be at least 1 (the command to execute), however it may be strictly
# greater than 1 if the command itself has options.
if [ "$#" -eq 0 ] || [ "$interval" -lt 0 ]
then
printUsage
exit 1
fi
# kill -0 pid Exit code indicates if a signal may be sent to $pid process.
(
: $((t = timeout))
while [ "$t" -gt 0 ]; do
sleep $interval
kill -0 $$ || exit 0
: $((t -= interval))
done
# Be nice, post SIGTERM first.
# The 'exit 0' below will be executed if any preceeding command fails.
kill -0 $$ || exit 0
if [ "${log}" ]
then
touch ${log}
fi
kill -s TERM $$ && exit 143
sleep $delay
kill -s KILL $$
exit 137 # mimic SIGKILL ec
) 2> /dev/null &
exec "$@"