#!/usr/bin/env ruby require 'timeout' puts 'starting process' pid = Process.spawn('sleep 20') begin Timeout.timeout(5) do puts 'waiting for the process to end' Process.wait(pid) puts 'process finished in time' end rescue Timeout::Error puts 'process not finished in time, killing it' Process.kill('TERM', pid) end