Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Created September 11, 2013 18:18
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jaytaylor/6527607 to your computer and use it in GitHub Desktop.
Save jaytaylor/6527607 to your computer and use it in GitHub Desktop.
Mac OS-X does not come with the delightfully useful `timeout` program. Thankfully a rough BASH equivalent can be achieved with only 2 perl statements.
#
# Mac OS-X does not come with the delightfully useful `timeout` program. Thankfully a rough BASH equivalent can be achieved with only 2 perl statements.
#
# Originally found on SO: http://stackoverflow.com/questions/601543/command-line-command-to-auto-kill-a-command-after-a-certain-amount-of-time
#
function timeout() { perl -e 'alarm shift; exec @ARGV' "$@"; }
## Example usage:
#
# $ function timeout() { perl -e 'alarm shift; exec @ARGV' "$@"; }
#
# $ timeout 1 sleep 2; echo $?
# Alarm clock: 14
# 142
#
# $ timeout 1 sleep 0.5; echo $?
# 0
#
# $ timeout 1 bash -c 'echo "hi" && sleep 2 && echo "bye"'; echo $?
# hi
# Alarm clock: 14
# 142
#
# $ timeout 3 bash -c 'echo "hi" && sleep 2 && echo "bye"'; echo $?
# hi
# bye
# 0
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment