Last active
April 18, 2017 14:37
-
-
Save JayGoldberg/41ccf3f2548d7416bc9398f4d9382a3b to your computer and use it in GitHub Desktop.
Implement timeout command for systems that don't have it
This file contains hidden or 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
| ## @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