Skip to content

Instantly share code, notes, and snippets.

# Runs a specified shell command in a separate thread.
# If it exceeds the given timeout in seconds, kills it.
# Passes stdout, stderr, thread, and a boolean indicating a timeout occurred to the passed in block.
# Uses Kernel.select to wait up to the tick length (in seconds) between
# checks on the command's status
#
# If you've got a cleaner way of doing this, I'd be interested to see it.
# If you think you can do it with Ruby's Timeout module, think again.
def run_with_timeout(*command)
options = command.extract_options!.reverse_merge(timeout: 60, tick: 1, cleanup_sleep: 0.1, buffer_size: 10240)