#!/usr/bin/env ruby require 'timeout' cmd = "for i in {1..2000000}; do echo '123456789'; done" pipe_in, pipe_out = IO.pipe pid = Process.spawn(cmd, :out => pipe_out, :err => pipe_out) begin Timeout.timeout(1) { Process.wait pid } rescue Timeout::Error puts 'process not finished in time, killing it' Process.kill('TERM', pid) end pipe_out.close puts pipe_in.read