Skip to content

Instantly share code, notes, and snippets.

@JayGoldberg
Last active April 18, 2017 14:37
Show Gist options
  • Save JayGoldberg/41ccf3f2548d7416bc9398f4d9382a3b to your computer and use it in GitHub Desktop.
Save JayGoldberg/41ccf3f2548d7416bc9398f4d9382a3b to your computer and use it in GitHub Desktop.
Implement timeout command for systems that don't have it
## @author Jay Goldberg
## @email [email protected]
## @license Apache 2.0
## @description Implement timeout for systems that don't have it
## @usage source it, then timeout <secs> <command>
#=======================================================================
timeout() {
local timeout_secs=${1:-10}
shift
[ ! -z "${timeout_secs//[0-9]}" ] && { return 65; }
# subshell
(
"$@" &
child=$! #trap - '' SIGTERM #why would we need this?
(
sleep $timeout_secs
kill $child 2> /dev/null # TODO returns 143 instead of "real" timeout's 124
) &
wait $child
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment