Skip to content

Instantly share code, notes, and snippets.

@honzahommer
Created February 24, 2019 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honzahommer/1c97eb70098618c6b16da8ed6a840188 to your computer and use it in GitHub Desktop.
Save honzahommer/1c97eb70098618c6b16da8ed6a840188 to your computer and use it in GitHub Desktop.
timeout.sh - run a command with a time limit
#!/bin/sh
duration="${1}"; shift
if [ "$#" -eq 0 ] || [ "$(echo "$duration" | tr -dc '0-9')" != "$duration" ]; then
echo "Usage: $0 DURATION COMMAND [ARG]..."
exit 1
fi
( "$@" &
child_pid=$!
trap -- "" SIGTERM
( sleep $duration
kill $child_pid 2> /dev/null ) &
wait $child_pid
)
unset -f duration child_pid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment