Last active
September 21, 2020 15:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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