Search code examples
bashtimeoutsox

timeout command not timing out


I have some issues with timing out a long-running sox recording.

The command is needed because sox gets stdout from a process, but it's not a permanently flow of data (but has interruptions with silence). But as sox is told to record 30s in total, it records until the audio file has 30s of sound. So I want the command to timeout after 35s if it's not finished before.

Used command in bash script:

timeout 35s sox [--some --sox --parameters] trim 0 30

Now the problem is that the given command may take anything between 35s and >10min and I have no idea why.

It seems completely random how long it takes, and audio files are something between 1s and 13s.

So maybe someone here has an idea, probably how to troubleshoot etc


Solution

  • The terminate signal handler in sox just sets the flag user_abort, which is checked by the Callback for monitoring flow progress once per buffer, so sox waiting until a buffer gets full can explain the long delay for honoring the signal. The situation might improve if you specify a small buffer size, but that's not a solution.

    I now noticed that you wrote sox gets stdout from a process. If you timeout that process rather than sox, the closing of its input source should cause sox to instantly terminate.

    Unfortunately that's not a suitable solution as the "main process" (sox gets the stdout from) runs 24/7 and shouldn't be interrupted.

    You could instead connect cat to the input and then timeout 35 cat | sox ….