I'm looking for a lightweight solution to kill a command with output redirection if it hangs. my current solution
if [[ -w /tmp/mypipe ]]; then
timeout --kill-after 10 5 echo "1" > /tmp/mypipe
fi
works only if the left part of the command does not work correctly (e.g. no one does read from the pipe). But I discovered situation, where the redirection hangs -- which at least are an issue of not completely synchronized tasks, which I can't solve now.
There are several related questions like this, this or that. The last one mostly covers my question, but I'm still looking for a more slim solution. It suggests to work like
( CMDPID=$BASHPID; \
(sleep 5; kill -9 $CMDPID >& /dev/null) & echo "1" > /tmp/mypipe )
But this spawns two new bash processes. Is there a more lightweight solution for that problem?