Created
July 17, 2020 14:27
-
-
Save jcbf/56ce19814ebd3f76cb0fde466d8581fe to your computer and use it in GitHub Desktop.
bash timeout
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
./run.sh 10 slow_command | |
./run.sh 3 sleep 5 |
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
#!/bin/bash | |
# run | |
# Run command with timeout $1 seconds. | |
# Timeout seconds | |
timeout_seconds="$1" | |
shift | |
# PID | |
pid=$$ | |
# Start timeout | |
( | |
sleep "$timeout_seconds" | |
echo "Timed out after $timeout_seconds seconds" | |
kill -- -$pid &>/dev/null | |
) & | |
timeout_pid=$! | |
# Run | |
"$@" | |
# Stop timeout | |
kill $timeout_pid &>/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment