4 Matching Annotations
- Feb 2021
-
stackoverflow.com stackoverflow.com
-
# Usage: run_with_timeout N cmd args... # or: run_with_timeout cmd args... # In the second case, cmd cannot be a number and the timeout will be 10 seconds. run_with_timeout () { local time=10 if [[ $1 =~ ^[0-9]+$ ]]; then time=$1; shift; fi # Run in a subshell to avoid job control messages ( "$@" & child=$! # Avoid default notification in non-interactive shell for SIGTERM trap -- "" SIGTERM ( sleep $time kill $child 2> /dev/null ) & wait $child ) }
-
-
devel.ringlet.net devel.ringlet.net
Tags
Annotators
URL
-
-
www.howtogeek.com www.howtogeek.com
-
We can ask timeout to try to stop the program using SIGTERM, and to only send in SIGKILL if SIGTERM didn’t work. To do this, we use the -k (kill after) option. The -k option requires a time value as a parameter.
-
-
stackoverflow.com stackoverflow.com
-
timeout_child () { trap -- "" SIGTERM; child=$!; timeout=$1; ( sleep $timeout; kill $child; ) & wait $child; } And the usage: ( while true; do echo -n .; sleep 0.1; done) & timeout_child 2
-