Sometimes one needs to terminate a command after a period of time and this particular command does not offer a timeout function itself. This could be a real show-stopper when it’s about running commands synchronously inside of scripts.
Somehow I didn’t manage to get “timeout” from the coreutils working correctly inside of a bash script while it always worked when invoked directly from the shell.
At last I found a working solution here, this one even works with built-in commands bash offers:
<your command> & read -t <timeout in seconds> ; kill $!
For example running a “socat” UDP-listener on port 5555 for 10 seconds:
socat - udp4-listen:5555,reuseaddr,fork 2>/dev/null & read -t 10 ; kill $!
This solution has a nice side-effect: It works without creating a subshell process. This makes passing variables and re-directing in and out a lot easier.