require 'timeout'
Timeout.timeout(<seconds>) do
output = `casperjs #{path_to_file} '#{url}#'`
end
This will raise a Timeout::Error exception if the script doesn't finish within the given time
Edit(to kill the process after timeout)
require 'timeout'
pipe_cmd_in, pipe_cmd_out = IO.pipe
pid = Process.spawn("casperjs #{path_to_file} '#{url}#'", :out => pipe_cmd_out, :err => pipe_cmd_out)
Timeout.timeout(<seconds>) do
Process.wait(pid)
pipe_cmd_out.close
output = pipe_cmd_in.read;
end
rescue Timeout::Error
Process.kill('TERM', pid)
end
Have a look at the teminator gem as well. It will help if you have many system calls to deal with