Created
February 24, 2019 05:55
-
-
Save honzahommer/1c97eb70098618c6b16da8ed6a840188 to your computer and use it in GitHub Desktop.
timeout.sh - run a command with a time limit
This file contains 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
#!/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