Skip to content

Instantly share code, notes, and snippets.

@PhillyPhoto
Forked from jaytaylor/timeout.sh
Created September 3, 2025 00:42
Show Gist options
  • Select an option

  • Save PhillyPhoto/f95d7bc577301c7b40923c23ae944e8b to your computer and use it in GitHub Desktop.

Select an option

Save PhillyPhoto/f95d7bc577301c7b40923c23ae944e8b 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