I need to run a command and kill it when running too long, in a bash script. I also need to capture all output to a variable. If the command finishes first, I need to release/kill the watchdog process (e.g. sleep) because I may run a list of such commands.
Unfortunately the "timeout" command is not available to me, othervise I could do something like this:
output=`timeout -s 9 $TIMEOUT my-command`
and check for the exit code 124 to see if there was a timeout.
Therefore my solution of choice is by @Dmitry to a similar question:
( my_command ) & pid=$!
( sleep $TIMEOUT && kill -HUP $pid ) 2>/dev/null & watcher=$!
wait $pid 2>/dev/null && pkill -HUP -P $watcher
Unfortunately the following does not capture anything to the $output:
( output=`my_command` ) & pid=$!
I could dump the output to a file and then load it in the variable like this, but I'd rather do without files:
( `my_command >$outfile` ) & pid=$!
...
output=`cat $outfile`
rm -f $outfile
My question: is there a better way? Ideally capturing the stderr as well to another variable without using files?