Running this command in an interactive bash:
$ timeout 1 sleep 2; echo $?
124
returns 124 after 1 second, as expected and as documented in timeout(1).
However, if I run the same as a cron job, or if I give that as a command string to bash, it does not:
$ bash -c "timeout 1 sleep 2; echo $?"
0
Adding -i
to the bash invocation does not help, neither does using the --foreground
parameter to timeout(1). I also tried the same with ksh and zsh, but always get the same result, so I guess it must be something inherent to the way timeout(1) works.
I searched the net a bit and found, that it could have to do with how the signalling proceeds through the process groups, but I could not find a solution how to make timeout work as expected in the non-interactive case.
Any hints on how I could achieve that? Ultimately what I want is to run a command in cron that is likely to block forever and I want to detect that case reliably.