Skip to content

Instantly share code, notes, and snippets.

@sbliven
Last active September 21, 2020 15:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sbliven/0da4eba5380ba514accb4118d481fbc9 to your computer and use it in GitHub Desktop.
Save sbliven/0da4eba5380ba514accb4118d481fbc9 to your computer and use it in GitHub Desktop.
'timeout' command, implemented within the shell so that it works with shell functions
#!/bin/zsh
# A version of the 'timeout' command that works with shell functions
#
# Usage:
# source timeout_fn.sh
# timeout_fn DURATION COMMAND [ARG]...
timeout_fn () {
local timeout=$1
shift
eval "$@" &
SLOW_PID=$!
{ `#timeout` sleep $timeout; kill $SLOW_PID 2>&1 | grep -v 'no such process' >&2; exit 124; } &
wait $SLOW_PID
}
## Test only
#
#test_fn () {
# echo start
# sleep $1
# echo done
# return 1
#}
#
#echo slow command:
#timeout_fn 1 test_fn 5
#echo Returned: $?
#
#echo fast command:
#timeout_fn 5 test_fn 1
#echo Returned: $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment