Reputation: 303
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?
Upvotes: 2
Views: 760
Reputation: 123410
Keep in mind that each spawned bash process will share the vast majority of its memory with the parent process, so it's not tripling memory usage like top would have you believe.
If you still want to try to optimize, timeout 5 tee /tmp/mypipe <<< "1" > /dev/null
will spawn a timeout and a tee process instead.
Upvotes: 1