Pádraig Brady <p...@draigbrady.com> writes: > A bit of an unusual use case. > What about the albeit slightly hacky solution presented in that SO thread? > > timeout --foreground 3 sh -c 'possibly_slow_program;exit'
I have the sense that I've stumbled into this as a problem more often than I expect, given how unusual a situation it seems like it should be. At this point, the best proposals I know of are: (1) As mentioned in https://unix.stackexchange.com/questions/84973/timeout-without-killing-process-in-bash > timeout --foreground 3 sh -c 'possibly_slow_program;exit' (2) Also in that thread: > possibly_slow_program & J1=$! ; sleep 3 & J2=$! ; wait -n $J1 $J2 which is conceptually simple but syntactically messy. (3) The proposed > timeout --signal=0 --no-wait (4) There is also the possiblity of adding a timeout to Bash's "wait" built-in, which would yield the code: > possibly_slow_program & > wait -t 3 $! I thought of this a while ago, but it seemed to me that (1) the last thing Bash needs is another feature and (2) the timeout would be provided to "wait" via the same method as it is to "read", which I mistakenly thought was a shell variable. It seemed impossible to add a timeout via a shell variable to "wait" in an upward-compatible manner. But looking at the Bash documentation, I see that while there is a TMOUT variable that can provide a timeout to "read", there is a -t option which also does so. And it looks like adding -t to "wait", does not have those downsides. OTOH, it looks like implementing a timeout for "wait" would be complicated, as there are no Posix wait-for-child functions that have a timeout argument. So at this point, I still prefer (3). Dale