This will record as soon as non-silence is detected (with a 2% volume threshold). The recording will cease when silence is detected for 2 seconds or more.
Note that the program terminates when silence is detected, so if you wanted the program to listen for a second chunk of audio and continue the recording, you would have to call rec from a loop. This is fine, except that it will truncate the file it records to if you use the same file the next time.
One solution is to create a different file for each recording. This might be exactly what you want. If so, it's pretty easy to write a little script to do this. I added in recording for only an hour too, although it will continue to record if there is no silence after the last rec instance is started even if that goes over an hour.
Home page: http://sox.sourceforge.net/Git repository:
Modifications I want to make